isl_ast.c: fix typo in comment
[isl.git] / isl_coalesce.c
blobca304e7215fe1dbcbfa9a7edc8553ace524f9445
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
18 * CS 42112, 75589 Paris Cedex 12, France
21 #include <isl_ctx_private.h>
22 #include "isl_map_private.h"
23 #include <isl_seq.h>
24 #include <isl/options.h>
25 #include "isl_tab.h"
26 #include <isl_mat_private.h>
27 #include <isl_local_space_private.h>
28 #include <isl_val_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_equalities.h>
32 #include <isl_constraint_private.h>
34 #include <set_to_map.c>
35 #include <set_from_map.c>
37 #define STATUS_ERROR -1
38 #define STATUS_REDUNDANT 1
39 #define STATUS_VALID 2
40 #define STATUS_SEPARATE 3
41 #define STATUS_CUT 4
42 #define STATUS_ADJ_EQ 5
43 #define STATUS_ADJ_INEQ 6
45 static int status_in(isl_int *ineq, struct isl_tab *tab)
47 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
48 switch (type) {
49 default:
50 case isl_ineq_error: return STATUS_ERROR;
51 case isl_ineq_redundant: return STATUS_VALID;
52 case isl_ineq_separate: return STATUS_SEPARATE;
53 case isl_ineq_cut: return STATUS_CUT;
54 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
55 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
59 /* Compute the position of the equalities of basic map "bmap_i"
60 * with respect to the basic map represented by "tab_j".
61 * The resulting array has twice as many entries as the number
62 * of equalities corresponding to the two inequalities to which
63 * each equality corresponds.
65 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
66 struct isl_tab *tab_j)
68 int k, l;
69 int *eq;
70 isl_size dim;
72 dim = isl_basic_map_dim(bmap_i, isl_dim_all);
73 if (dim < 0)
74 return NULL;
76 eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
77 if (!eq)
78 return NULL;
80 for (k = 0; k < bmap_i->n_eq; ++k) {
81 for (l = 0; l < 2; ++l) {
82 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
83 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
84 if (eq[2 * k + l] == STATUS_ERROR)
85 goto error;
89 return eq;
90 error:
91 free(eq);
92 return NULL;
95 /* Compute the position of the inequalities of basic map "bmap_i"
96 * (also represented by "tab_i", if not NULL) with respect to the basic map
97 * represented by "tab_j".
99 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
100 struct isl_tab *tab_i, struct isl_tab *tab_j)
102 int k;
103 unsigned n_eq = bmap_i->n_eq;
104 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
106 if (!ineq)
107 return NULL;
109 for (k = 0; k < bmap_i->n_ineq; ++k) {
110 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
111 ineq[k] = STATUS_REDUNDANT;
112 continue;
114 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
115 if (ineq[k] == STATUS_ERROR)
116 goto error;
117 if (ineq[k] == STATUS_SEPARATE)
118 break;
121 return ineq;
122 error:
123 free(ineq);
124 return NULL;
127 static int any(int *con, unsigned len, int status)
129 int i;
131 for (i = 0; i < len ; ++i)
132 if (con[i] == status)
133 return 1;
134 return 0;
137 /* Return the first position of "status" in the list "con" of length "len".
138 * Return -1 if there is no such entry.
140 static int find(int *con, unsigned len, int status)
142 int i;
144 for (i = 0; i < len ; ++i)
145 if (con[i] == status)
146 return i;
147 return -1;
150 static int count(int *con, unsigned len, int status)
152 int i;
153 int c = 0;
155 for (i = 0; i < len ; ++i)
156 if (con[i] == status)
157 c++;
158 return c;
161 static int all(int *con, unsigned len, int status)
163 int i;
165 for (i = 0; i < len ; ++i) {
166 if (con[i] == STATUS_REDUNDANT)
167 continue;
168 if (con[i] != status)
169 return 0;
171 return 1;
174 /* Internal information associated to a basic map in a map
175 * that is to be coalesced by isl_map_coalesce.
177 * "bmap" is the basic map itself (or NULL if "removed" is set)
178 * "tab" is the corresponding tableau (or NULL if "removed" is set)
179 * "hull_hash" identifies the affine space in which "bmap" lives.
180 * "modified" is set if this basic map may not be identical
181 * to any of the basic maps in the input.
182 * "removed" is set if this basic map has been removed from the map
183 * "simplify" is set if this basic map may have some unknown integer
184 * divisions that were not present in the input basic maps. The basic
185 * map should then be simplified such that we may be able to find
186 * a definition among the constraints.
188 * "eq" and "ineq" are only set if we are currently trying to coalesce
189 * this basic map with another basic map, in which case they represent
190 * the position of the inequalities of this basic map with respect to
191 * the other basic map. The number of elements in the "eq" array
192 * is twice the number of equalities in the "bmap", corresponding
193 * to the two inequalities that make up each equality.
195 struct isl_coalesce_info {
196 isl_basic_map *bmap;
197 struct isl_tab *tab;
198 uint32_t hull_hash;
199 int modified;
200 int removed;
201 int simplify;
202 int *eq;
203 int *ineq;
206 /* Is there any (half of an) equality constraint in the description
207 * of the basic map represented by "info" that
208 * has position "status" with respect to the other basic map?
210 static int any_eq(struct isl_coalesce_info *info, int status)
212 unsigned n_eq;
214 n_eq = isl_basic_map_n_equality(info->bmap);
215 return any(info->eq, 2 * n_eq, status);
218 /* Is there any inequality constraint in the description
219 * of the basic map represented by "info" that
220 * has position "status" with respect to the other basic map?
222 static int any_ineq(struct isl_coalesce_info *info, int status)
224 unsigned n_ineq;
226 n_ineq = isl_basic_map_n_inequality(info->bmap);
227 return any(info->ineq, n_ineq, status);
230 /* Return the position of the first half on an equality constraint
231 * in the description of the basic map represented by "info" that
232 * has position "status" with respect to the other basic map.
233 * The returned value is twice the position of the equality constraint
234 * plus zero for the negative half and plus one for the positive half.
235 * Return -1 if there is no such entry.
237 static int find_eq(struct isl_coalesce_info *info, int status)
239 unsigned n_eq;
241 n_eq = isl_basic_map_n_equality(info->bmap);
242 return find(info->eq, 2 * n_eq, status);
245 /* Return the position of the first inequality constraint in the description
246 * of the basic map represented by "info" that
247 * has position "status" with respect to the other basic map.
248 * Return -1 if there is no such entry.
250 static int find_ineq(struct isl_coalesce_info *info, int status)
252 unsigned n_ineq;
254 n_ineq = isl_basic_map_n_inequality(info->bmap);
255 return find(info->ineq, n_ineq, status);
258 /* Return the number of (halves of) equality constraints in the description
259 * of the basic map represented by "info" that
260 * have position "status" with respect to the other basic map.
262 static int count_eq(struct isl_coalesce_info *info, int status)
264 unsigned n_eq;
266 n_eq = isl_basic_map_n_equality(info->bmap);
267 return count(info->eq, 2 * n_eq, status);
270 /* Return the number of inequality constraints in the description
271 * of the basic map represented by "info" that
272 * have position "status" with respect to the other basic map.
274 static int count_ineq(struct isl_coalesce_info *info, int status)
276 unsigned n_ineq;
278 n_ineq = isl_basic_map_n_inequality(info->bmap);
279 return count(info->ineq, n_ineq, status);
282 /* Are all non-redundant constraints of the basic map represented by "info"
283 * either valid or cut constraints with respect to the other basic map?
285 static int all_valid_or_cut(struct isl_coalesce_info *info)
287 int i;
289 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
290 if (info->eq[i] == STATUS_REDUNDANT)
291 continue;
292 if (info->eq[i] == STATUS_VALID)
293 continue;
294 if (info->eq[i] == STATUS_CUT)
295 continue;
296 return 0;
299 for (i = 0; i < info->bmap->n_ineq; ++i) {
300 if (info->ineq[i] == STATUS_REDUNDANT)
301 continue;
302 if (info->ineq[i] == STATUS_VALID)
303 continue;
304 if (info->ineq[i] == STATUS_CUT)
305 continue;
306 return 0;
309 return 1;
312 /* Compute the hash of the (apparent) affine hull of info->bmap (with
313 * the existentially quantified variables removed) and store it
314 * in info->hash.
316 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
318 isl_basic_map *hull;
319 isl_size n_div;
321 hull = isl_basic_map_copy(info->bmap);
322 hull = isl_basic_map_plain_affine_hull(hull);
323 n_div = isl_basic_map_dim(hull, isl_dim_div);
324 if (n_div < 0)
325 hull = isl_basic_map_free(hull);
326 hull = isl_basic_map_drop_constraints_involving_dims(hull,
327 isl_dim_div, 0, n_div);
328 info->hull_hash = isl_basic_map_get_hash(hull);
329 isl_basic_map_free(hull);
331 return hull ? 0 : -1;
334 /* Free all the allocated memory in an array
335 * of "n" isl_coalesce_info elements.
337 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
339 int i;
341 if (!info)
342 return;
344 for (i = 0; i < n; ++i) {
345 isl_basic_map_free(info[i].bmap);
346 isl_tab_free(info[i].tab);
349 free(info);
352 /* Clear the memory associated to "info".
354 static void clear(struct isl_coalesce_info *info)
356 info->bmap = isl_basic_map_free(info->bmap);
357 isl_tab_free(info->tab);
358 info->tab = NULL;
361 /* Drop the basic map represented by "info".
362 * That is, clear the memory associated to the entry and
363 * mark it as having been removed.
365 static void drop(struct isl_coalesce_info *info)
367 clear(info);
368 info->removed = 1;
371 /* Exchange the information in "info1" with that in "info2".
373 static void exchange(struct isl_coalesce_info *info1,
374 struct isl_coalesce_info *info2)
376 struct isl_coalesce_info info;
378 info = *info1;
379 *info1 = *info2;
380 *info2 = info;
383 /* This type represents the kind of change that has been performed
384 * while trying to coalesce two basic maps.
386 * isl_change_none: nothing was changed
387 * isl_change_drop_first: the first basic map was removed
388 * isl_change_drop_second: the second basic map was removed
389 * isl_change_fuse: the two basic maps were replaced by a new basic map.
391 enum isl_change {
392 isl_change_error = -1,
393 isl_change_none = 0,
394 isl_change_drop_first,
395 isl_change_drop_second,
396 isl_change_fuse,
399 /* Update "change" based on an interchange of the first and the second
400 * basic map. That is, interchange isl_change_drop_first and
401 * isl_change_drop_second.
403 static enum isl_change invert_change(enum isl_change change)
405 switch (change) {
406 case isl_change_error:
407 return isl_change_error;
408 case isl_change_none:
409 return isl_change_none;
410 case isl_change_drop_first:
411 return isl_change_drop_second;
412 case isl_change_drop_second:
413 return isl_change_drop_first;
414 case isl_change_fuse:
415 return isl_change_fuse;
418 return isl_change_error;
421 /* Add the valid constraints of the basic map represented by "info"
422 * to "bmap". "len" is the size of the constraints.
423 * If only one of the pair of inequalities that make up an equality
424 * is valid, then add that inequality.
426 static __isl_give isl_basic_map *add_valid_constraints(
427 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
428 unsigned len)
430 int k, l;
432 if (!bmap)
433 return NULL;
435 for (k = 0; k < info->bmap->n_eq; ++k) {
436 if (info->eq[2 * k] == STATUS_VALID &&
437 info->eq[2 * k + 1] == STATUS_VALID) {
438 l = isl_basic_map_alloc_equality(bmap);
439 if (l < 0)
440 return isl_basic_map_free(bmap);
441 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
442 } else if (info->eq[2 * k] == STATUS_VALID) {
443 l = isl_basic_map_alloc_inequality(bmap);
444 if (l < 0)
445 return isl_basic_map_free(bmap);
446 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
447 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
448 l = isl_basic_map_alloc_inequality(bmap);
449 if (l < 0)
450 return isl_basic_map_free(bmap);
451 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
455 for (k = 0; k < info->bmap->n_ineq; ++k) {
456 if (info->ineq[k] != STATUS_VALID)
457 continue;
458 l = isl_basic_map_alloc_inequality(bmap);
459 if (l < 0)
460 return isl_basic_map_free(bmap);
461 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
464 return bmap;
467 /* Is "bmap" defined by a number of (non-redundant) constraints that
468 * is greater than the number of constraints of basic maps i and j combined?
469 * Equalities are counted as two inequalities.
471 static int number_of_constraints_increases(int i, int j,
472 struct isl_coalesce_info *info,
473 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
475 int k, n_old, n_new;
477 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
478 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
480 n_new = 2 * bmap->n_eq;
481 for (k = 0; k < bmap->n_ineq; ++k)
482 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
483 ++n_new;
485 return n_new > n_old;
488 /* Replace the pair of basic maps i and j by the basic map bounded
489 * by the valid constraints in both basic maps and the constraints
490 * in extra (if not NULL).
491 * Place the fused basic map in the position that is the smallest of i and j.
493 * If "detect_equalities" is set, then look for equalities encoded
494 * as pairs of inequalities.
495 * If "check_number" is set, then the original basic maps are only
496 * replaced if the total number of constraints does not increase.
497 * While the number of integer divisions in the two basic maps
498 * is assumed to be the same, the actual definitions may be different.
499 * We only copy the definition from one of the basic map if it is
500 * the same as that of the other basic map. Otherwise, we mark
501 * the integer division as unknown and simplify the basic map
502 * in an attempt to recover the integer division definition.
504 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
505 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
507 int k, l;
508 struct isl_basic_map *fused = NULL;
509 struct isl_tab *fused_tab = NULL;
510 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
511 unsigned extra_rows = extra ? extra->n_row : 0;
512 unsigned n_eq, n_ineq;
513 int simplify = 0;
515 if (total < 0)
516 return isl_change_error;
517 if (j < i)
518 return fuse(j, i, info, extra, detect_equalities, check_number);
520 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
521 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
522 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
523 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
524 fused = add_valid_constraints(fused, &info[i], 1 + total);
525 fused = add_valid_constraints(fused, &info[j], 1 + total);
526 if (!fused)
527 goto error;
528 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
529 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
530 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
532 for (k = 0; k < info[i].bmap->n_div; ++k) {
533 int l = isl_basic_map_alloc_div(fused);
534 if (l < 0)
535 goto error;
536 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
537 1 + 1 + total)) {
538 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
539 1 + 1 + total);
540 } else {
541 isl_int_set_si(fused->div[l][0], 0);
542 simplify = 1;
546 for (k = 0; k < extra_rows; ++k) {
547 l = isl_basic_map_alloc_inequality(fused);
548 if (l < 0)
549 goto error;
550 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
553 if (detect_equalities)
554 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
555 fused = isl_basic_map_gauss(fused, NULL);
556 if (simplify || info[j].simplify) {
557 fused = isl_basic_map_simplify(fused);
558 info[i].simplify = 0;
560 fused = isl_basic_map_finalize(fused);
562 fused_tab = isl_tab_from_basic_map(fused, 0);
563 if (isl_tab_detect_redundant(fused_tab) < 0)
564 goto error;
566 if (check_number &&
567 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
568 isl_tab_free(fused_tab);
569 isl_basic_map_free(fused);
570 return isl_change_none;
573 clear(&info[i]);
574 info[i].bmap = fused;
575 info[i].tab = fused_tab;
576 info[i].modified = 1;
577 drop(&info[j]);
579 return isl_change_fuse;
580 error:
581 isl_tab_free(fused_tab);
582 isl_basic_map_free(fused);
583 return isl_change_error;
586 /* Given a pair of basic maps i and j such that all constraints are either
587 * "valid" or "cut", check if the facets corresponding to the "cut"
588 * constraints of i lie entirely within basic map j.
589 * If so, replace the pair by the basic map consisting of the valid
590 * constraints in both basic maps.
591 * Checking whether the facet lies entirely within basic map j
592 * is performed by checking whether the constraints of basic map j
593 * are valid for the facet. These tests are performed on a rational
594 * tableau to avoid the theoretical possibility that a constraint
595 * that was considered to be a cut constraint for the entire basic map i
596 * happens to be considered to be a valid constraint for the facet,
597 * even though it cuts off the same rational points.
599 * To see that we are not introducing any extra points, call the
600 * two basic maps A and B and the resulting map U and let x
601 * be an element of U \setminus ( A \cup B ).
602 * A line connecting x with an element of A \cup B meets a facet F
603 * of either A or B. Assume it is a facet of B and let c_1 be
604 * the corresponding facet constraint. We have c_1(x) < 0 and
605 * so c_1 is a cut constraint. This implies that there is some
606 * (possibly rational) point x' satisfying the constraints of A
607 * and the opposite of c_1 as otherwise c_1 would have been marked
608 * valid for A. The line connecting x and x' meets a facet of A
609 * in a (possibly rational) point that also violates c_1, but this
610 * is impossible since all cut constraints of B are valid for all
611 * cut facets of A.
612 * In case F is a facet of A rather than B, then we can apply the
613 * above reasoning to find a facet of B separating x from A \cup B first.
615 static enum isl_change check_facets(int i, int j,
616 struct isl_coalesce_info *info)
618 int k, l;
619 struct isl_tab_undo *snap, *snap2;
620 unsigned n_eq = info[i].bmap->n_eq;
622 snap = isl_tab_snap(info[i].tab);
623 if (isl_tab_mark_rational(info[i].tab) < 0)
624 return isl_change_error;
625 snap2 = isl_tab_snap(info[i].tab);
627 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
628 if (info[i].ineq[k] != STATUS_CUT)
629 continue;
630 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
631 return isl_change_error;
632 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
633 int stat;
634 if (info[j].ineq[l] != STATUS_CUT)
635 continue;
636 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
637 if (stat < 0)
638 return isl_change_error;
639 if (stat != STATUS_VALID)
640 break;
642 if (isl_tab_rollback(info[i].tab, snap2) < 0)
643 return isl_change_error;
644 if (l < info[j].bmap->n_ineq)
645 break;
648 if (k < info[i].bmap->n_ineq) {
649 if (isl_tab_rollback(info[i].tab, snap) < 0)
650 return isl_change_error;
651 return isl_change_none;
653 return fuse(i, j, info, NULL, 0, 0);
656 /* Check if info->bmap contains the basic map represented
657 * by the tableau "tab".
658 * For each equality, we check both the constraint itself
659 * (as an inequality) and its negation. Make sure the
660 * equality is returned to its original state before returning.
662 static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
664 int k;
665 isl_size dim;
666 isl_basic_map *bmap = info->bmap;
668 dim = isl_basic_map_dim(bmap, isl_dim_all);
669 if (dim < 0)
670 return isl_bool_error;
671 for (k = 0; k < bmap->n_eq; ++k) {
672 int stat;
673 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
674 stat = status_in(bmap->eq[k], tab);
675 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
676 if (stat < 0)
677 return isl_bool_error;
678 if (stat != STATUS_VALID)
679 return isl_bool_false;
680 stat = status_in(bmap->eq[k], tab);
681 if (stat < 0)
682 return isl_bool_error;
683 if (stat != STATUS_VALID)
684 return isl_bool_false;
687 for (k = 0; k < bmap->n_ineq; ++k) {
688 int stat;
689 if (info->ineq[k] == STATUS_REDUNDANT)
690 continue;
691 stat = status_in(bmap->ineq[k], tab);
692 if (stat < 0)
693 return isl_bool_error;
694 if (stat != STATUS_VALID)
695 return isl_bool_false;
697 return isl_bool_true;
700 /* Basic map "i" has an inequality (say "k") that is adjacent
701 * to some inequality of basic map "j". All the other inequalities
702 * are valid for "j".
703 * Check if basic map "j" forms an extension of basic map "i".
705 * Note that this function is only called if some of the equalities or
706 * inequalities of basic map "j" do cut basic map "i". The function is
707 * correct even if there are no such cut constraints, but in that case
708 * the additional checks performed by this function are overkill.
710 * In particular, we replace constraint k, say f >= 0, by constraint
711 * f <= -1, add the inequalities of "j" that are valid for "i"
712 * and check if the result is a subset of basic map "j".
713 * To improve the chances of the subset relation being detected,
714 * any variable that only attains a single integer value
715 * in the tableau of "i" is first fixed to that value.
716 * If the result is a subset, then we know that this result is exactly equal
717 * to basic map "j" since all its constraints are valid for basic map "j".
718 * By combining the valid constraints of "i" (all equalities and all
719 * inequalities except "k") and the valid constraints of "j" we therefore
720 * obtain a basic map that is equal to their union.
721 * In this case, there is no need to perform a rollback of the tableau
722 * since it is going to be destroyed in fuse().
725 * |\__ |\__
726 * | \__ | \__
727 * | \_ => | \__
728 * |_______| _ |_________\
731 * |\ |\
732 * | \ | \
733 * | \ | \
734 * | | | \
735 * | ||\ => | \
736 * | || \ | \
737 * | || | | |
738 * |__||_/ |_____/
740 static enum isl_change is_adj_ineq_extension(int i, int j,
741 struct isl_coalesce_info *info)
743 int k;
744 struct isl_tab_undo *snap;
745 unsigned n_eq = info[i].bmap->n_eq;
746 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
747 isl_stat r;
748 isl_bool super;
750 if (total < 0)
751 return isl_change_error;
752 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
753 return isl_change_error;
755 k = find_ineq(&info[i], STATUS_ADJ_INEQ);
756 if (k < 0)
757 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
758 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
759 return isl_change_error);
761 snap = isl_tab_snap(info[i].tab);
763 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
764 return isl_change_error;
766 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
767 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
768 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
769 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
770 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
771 if (r < 0)
772 return isl_change_error;
774 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
775 if (info[j].ineq[k] != STATUS_VALID)
776 continue;
777 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
778 return isl_change_error;
780 if (isl_tab_detect_constants(info[i].tab) < 0)
781 return isl_change_error;
783 super = contains(&info[j], info[i].tab);
784 if (super < 0)
785 return isl_change_error;
786 if (super)
787 return fuse(i, j, info, NULL, 0, 0);
789 if (isl_tab_rollback(info[i].tab, snap) < 0)
790 return isl_change_error;
792 return isl_change_none;
796 /* Both basic maps have at least one inequality with and adjacent
797 * (but opposite) inequality in the other basic map.
798 * Check that there are no cut constraints and that there is only
799 * a single pair of adjacent inequalities.
800 * If so, we can replace the pair by a single basic map described
801 * by all but the pair of adjacent inequalities.
802 * Any additional points introduced lie strictly between the two
803 * adjacent hyperplanes and can therefore be integral.
805 * ____ _____
806 * / ||\ / \
807 * / || \ / \
808 * \ || \ => \ \
809 * \ || / \ /
810 * \___||_/ \_____/
812 * The test for a single pair of adjancent inequalities is important
813 * for avoiding the combination of two basic maps like the following
815 * /|
816 * / |
817 * /__|
818 * _____
819 * | |
820 * | |
821 * |___|
823 * If there are some cut constraints on one side, then we may
824 * still be able to fuse the two basic maps, but we need to perform
825 * some additional checks in is_adj_ineq_extension.
827 static enum isl_change check_adj_ineq(int i, int j,
828 struct isl_coalesce_info *info)
830 int count_i, count_j;
831 int cut_i, cut_j;
833 count_i = count_ineq(&info[i], STATUS_ADJ_INEQ);
834 count_j = count_ineq(&info[j], STATUS_ADJ_INEQ);
836 if (count_i != 1 && count_j != 1)
837 return isl_change_none;
839 cut_i = any_eq(&info[i], STATUS_CUT) || any_ineq(&info[i], STATUS_CUT);
840 cut_j = any_eq(&info[j], STATUS_CUT) || any_ineq(&info[j], STATUS_CUT);
842 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
843 return fuse(i, j, info, NULL, 0, 0);
845 if (count_i == 1 && !cut_i)
846 return is_adj_ineq_extension(i, j, info);
848 if (count_j == 1 && !cut_j)
849 return is_adj_ineq_extension(j, i, info);
851 return isl_change_none;
854 /* Given an affine transformation matrix "T", does row "row" represent
855 * anything other than a unit vector (possibly shifted by a constant)
856 * that is not involved in any of the other rows?
858 * That is, if a constraint involves the variable corresponding to
859 * the row, then could its preimage by "T" have any coefficients
860 * that are different from those in the original constraint?
862 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
864 int i, j;
865 int len = T->n_col - 1;
867 i = isl_seq_first_non_zero(T->row[row] + 1, len);
868 if (i < 0)
869 return 1;
870 if (!isl_int_is_one(T->row[row][1 + i]) &&
871 !isl_int_is_negone(T->row[row][1 + i]))
872 return 1;
874 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
875 if (j >= 0)
876 return 1;
878 for (j = 1; j < T->n_row; ++j) {
879 if (j == row)
880 continue;
881 if (!isl_int_is_zero(T->row[j][1 + i]))
882 return 1;
885 return 0;
888 /* Does inequality constraint "ineq" of "bmap" involve any of
889 * the variables marked in "affected"?
890 * "total" is the total number of variables, i.e., the number
891 * of entries in "affected".
893 static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
894 int *affected, int total)
896 int i;
898 for (i = 0; i < total; ++i) {
899 if (!affected[i])
900 continue;
901 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
902 return isl_bool_true;
905 return isl_bool_false;
908 /* Given the compressed version of inequality constraint "ineq"
909 * of info->bmap in "v", check if the constraint can be tightened,
910 * where the compression is based on an equality constraint valid
911 * for info->tab.
912 * If so, add the tightened version of the inequality constraint
913 * to info->tab. "v" may be modified by this function.
915 * That is, if the compressed constraint is of the form
917 * m f() + c >= 0
919 * with 0 < c < m, then it is equivalent to
921 * f() >= 0
923 * This means that c can also be subtracted from the original,
924 * uncompressed constraint without affecting the integer points
925 * in info->tab. Add this tightened constraint as an extra row
926 * to info->tab to make this information explicitly available.
928 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
929 int ineq, __isl_take isl_vec *v)
931 isl_ctx *ctx;
932 isl_stat r;
934 if (!v)
935 return NULL;
937 ctx = isl_vec_get_ctx(v);
938 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
939 if (isl_int_is_zero(ctx->normalize_gcd) ||
940 isl_int_is_one(ctx->normalize_gcd)) {
941 return v;
944 v = isl_vec_cow(v);
945 if (!v)
946 return NULL;
948 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
949 if (isl_int_is_zero(v->el[0]))
950 return v;
952 if (isl_tab_extend_cons(info->tab, 1) < 0)
953 return isl_vec_free(v);
955 isl_int_sub(info->bmap->ineq[ineq][0],
956 info->bmap->ineq[ineq][0], v->el[0]);
957 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
958 isl_int_add(info->bmap->ineq[ineq][0],
959 info->bmap->ineq[ineq][0], v->el[0]);
961 if (r < 0)
962 return isl_vec_free(v);
964 return v;
967 /* Tighten the (non-redundant) constraints on the facet represented
968 * by info->tab.
969 * In particular, on input, info->tab represents the result
970 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
971 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
972 * replacing the one at index "l" by the corresponding equality,
973 * i.e., f_k + 1 = 0, with k = relaxed[l].
975 * Compute a variable compression from the equality constraint f_k + 1 = 0
976 * and use it to tighten the other constraints of info->bmap
977 * (that is, all constraints that have not been relaxed),
978 * updating info->tab (and leaving info->bmap untouched).
979 * The compression handles essentially two cases, one where a variable
980 * is assigned a fixed value and can therefore be eliminated, and one
981 * where one variable is a shifted multiple of some other variable and
982 * can therefore be replaced by that multiple.
983 * Gaussian elimination would also work for the first case, but for
984 * the second case, the effectiveness would depend on the order
985 * of the variables.
986 * After compression, some of the constraints may have coefficients
987 * with a common divisor. If this divisor does not divide the constant
988 * term, then the constraint can be tightened.
989 * The tightening is performed on the tableau info->tab by introducing
990 * extra (temporary) constraints.
992 * Only constraints that are possibly affected by the compression are
993 * considered. In particular, if the constraint only involves variables
994 * that are directly mapped to a distinct set of other variables, then
995 * no common divisor can be introduced and no tightening can occur.
997 * It is important to only consider the non-redundant constraints
998 * since the facet constraint has been relaxed prior to the call
999 * to this function, meaning that the constraints that were redundant
1000 * prior to the relaxation may no longer be redundant.
1001 * These constraints will be ignored in the fused result, so
1002 * the fusion detection should not exploit them.
1004 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
1005 int n, int *relaxed, int l)
1007 isl_size total;
1008 isl_ctx *ctx;
1009 isl_vec *v = NULL;
1010 isl_mat *T;
1011 int i;
1012 int k;
1013 int *affected;
1015 k = relaxed[l];
1016 ctx = isl_basic_map_get_ctx(info->bmap);
1017 total = isl_basic_map_dim(info->bmap, isl_dim_all);
1018 if (total < 0)
1019 return isl_stat_error;
1020 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
1021 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
1022 T = isl_mat_variable_compression(T, NULL);
1023 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
1024 if (!T)
1025 return isl_stat_error;
1026 if (T->n_col == 0) {
1027 isl_mat_free(T);
1028 return isl_stat_ok;
1031 affected = isl_alloc_array(ctx, int, total);
1032 if (!affected)
1033 goto error;
1035 for (i = 0; i < total; ++i)
1036 affected[i] = not_unique_unit_row(T, 1 + i);
1038 for (i = 0; i < info->bmap->n_ineq; ++i) {
1039 isl_bool handle;
1040 if (any(relaxed, n, i))
1041 continue;
1042 if (info->ineq[i] == STATUS_REDUNDANT)
1043 continue;
1044 handle = is_affected(info->bmap, i, affected, total);
1045 if (handle < 0)
1046 goto error;
1047 if (!handle)
1048 continue;
1049 v = isl_vec_alloc(ctx, 1 + total);
1050 if (!v)
1051 goto error;
1052 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
1053 v = isl_vec_mat_product(v, isl_mat_copy(T));
1054 v = try_tightening(info, i, v);
1055 isl_vec_free(v);
1056 if (!v)
1057 goto error;
1060 isl_mat_free(T);
1061 free(affected);
1062 return isl_stat_ok;
1063 error:
1064 isl_mat_free(T);
1065 free(affected);
1066 return isl_stat_error;
1069 /* Replace the basic maps "i" and "j" by an extension of "i"
1070 * along the "n" inequality constraints in "relax" by one.
1071 * The tableau info[i].tab has already been extended.
1072 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
1073 * by one.
1074 * Each integer division that does not have exactly the same
1075 * definition in "i" and "j" is marked unknown and the basic map
1076 * is scheduled to be simplified in an attempt to recover
1077 * the integer division definition.
1078 * Place the extension in the position that is the smallest of i and j.
1080 static enum isl_change extend(int i, int j, int n, int *relax,
1081 struct isl_coalesce_info *info)
1083 int l;
1084 isl_size total;
1086 info[i].bmap = isl_basic_map_cow(info[i].bmap);
1087 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1088 if (total < 0)
1089 return isl_change_error;
1090 for (l = 0; l < info[i].bmap->n_div; ++l)
1091 if (!isl_seq_eq(info[i].bmap->div[l],
1092 info[j].bmap->div[l], 1 + 1 + total)) {
1093 isl_int_set_si(info[i].bmap->div[l][0], 0);
1094 info[i].simplify = 1;
1096 for (l = 0; l < n; ++l)
1097 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
1098 info[i].bmap->ineq[relax[l]][0], 1);
1099 ISL_F_CLR(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1100 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1101 drop(&info[j]);
1102 info[i].modified = 1;
1103 if (j < i)
1104 exchange(&info[i], &info[j]);
1105 return isl_change_fuse;
1108 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1109 * that are such that they include basic map "j" if they are relaxed
1110 * by one. All the other inequalities are valid for "j".
1111 * Check if basic map "j" forms an extension of basic map "i".
1113 * In particular, relax the constraints in "relax", compute the corresponding
1114 * facets one by one and check whether each of these is included
1115 * in the other basic map.
1116 * Before testing for inclusion, the constraints on each facet
1117 * are tightened to increase the chance of an inclusion being detected.
1118 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1119 * in is_adj_ineq_extension, may further increase those chances, but this
1120 * is not currently done.)
1121 * If each facet is included, we know that relaxing the constraints extends
1122 * the basic map with exactly the other basic map (we already know that this
1123 * other basic map is included in the extension, because all other
1124 * inequality constraints are valid of "j") and we can replace the
1125 * two basic maps by this extension.
1127 * If any of the relaxed constraints turn out to be redundant, then bail out.
1128 * isl_tab_select_facet refuses to handle such constraints. It may be
1129 * possible to handle them anyway by making a distinction between
1130 * redundant constraints with a corresponding facet that still intersects
1131 * the set (allowing isl_tab_select_facet to handle them) and
1132 * those where the facet does not intersect the set (which can be ignored
1133 * because the empty facet is trivially included in the other disjunct).
1134 * However, relaxed constraints that turn out to be redundant should
1135 * be fairly rare and no such instance has been reported where
1136 * coalescing would be successful.
1137 * ____ _____
1138 * / || / |
1139 * / || / |
1140 * \ || => \ |
1141 * \ || \ |
1142 * \___|| \____|
1145 * \ |\
1146 * |\\ | \
1147 * | \\ | \
1148 * | | => | /
1149 * | / | /
1150 * |/ |/
1152 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1153 struct isl_coalesce_info *info)
1155 int l;
1156 isl_bool super;
1157 struct isl_tab_undo *snap, *snap2;
1158 unsigned n_eq = info[i].bmap->n_eq;
1160 for (l = 0; l < n; ++l)
1161 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1162 return isl_change_none;
1164 snap = isl_tab_snap(info[i].tab);
1165 for (l = 0; l < n; ++l)
1166 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1167 return isl_change_error;
1168 for (l = 0; l < n; ++l) {
1169 if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
1170 continue;
1171 if (isl_tab_rollback(info[i].tab, snap) < 0)
1172 return isl_change_error;
1173 return isl_change_none;
1175 snap2 = isl_tab_snap(info[i].tab);
1176 for (l = 0; l < n; ++l) {
1177 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1178 return isl_change_error;
1179 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1180 return isl_change_error;
1181 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1182 return isl_change_error;
1183 super = contains(&info[j], info[i].tab);
1184 if (super < 0)
1185 return isl_change_error;
1186 if (super)
1187 continue;
1188 if (isl_tab_rollback(info[i].tab, snap) < 0)
1189 return isl_change_error;
1190 return isl_change_none;
1193 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1194 return isl_change_error;
1195 return extend(i, j, n, relax, info);
1198 /* Data structure that keeps track of the wrapping constraints
1199 * and of information to bound the coefficients of those constraints.
1201 * bound is set if we want to apply a bound on the coefficients
1202 * mat contains the wrapping constraints
1203 * max is the bound on the coefficients (if bound is set)
1205 struct isl_wraps {
1206 int bound;
1207 isl_mat *mat;
1208 isl_int max;
1211 /* Update wraps->max to be greater than or equal to the coefficients
1212 * in the equalities and inequalities of info->bmap that can be removed
1213 * if we end up applying wrapping.
1215 static isl_stat wraps_update_max(struct isl_wraps *wraps,
1216 struct isl_coalesce_info *info)
1218 int k;
1219 isl_int max_k;
1220 isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
1222 if (total < 0)
1223 return isl_stat_error;
1224 isl_int_init(max_k);
1226 for (k = 0; k < info->bmap->n_eq; ++k) {
1227 if (info->eq[2 * k] == STATUS_VALID &&
1228 info->eq[2 * k + 1] == STATUS_VALID)
1229 continue;
1230 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1231 if (isl_int_abs_gt(max_k, wraps->max))
1232 isl_int_set(wraps->max, max_k);
1235 for (k = 0; k < info->bmap->n_ineq; ++k) {
1236 if (info->ineq[k] == STATUS_VALID ||
1237 info->ineq[k] == STATUS_REDUNDANT)
1238 continue;
1239 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1240 if (isl_int_abs_gt(max_k, wraps->max))
1241 isl_int_set(wraps->max, max_k);
1244 isl_int_clear(max_k);
1246 return isl_stat_ok;
1249 /* Initialize the isl_wraps data structure.
1250 * If we want to bound the coefficients of the wrapping constraints,
1251 * we set wraps->max to the largest coefficient
1252 * in the equalities and inequalities that can be removed if we end up
1253 * applying wrapping.
1255 static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1256 struct isl_coalesce_info *info, int i, int j)
1258 isl_ctx *ctx;
1260 wraps->bound = 0;
1261 wraps->mat = mat;
1262 if (!mat)
1263 return isl_stat_error;
1264 ctx = isl_mat_get_ctx(mat);
1265 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1266 if (!wraps->bound)
1267 return isl_stat_ok;
1268 isl_int_init(wraps->max);
1269 isl_int_set_si(wraps->max, 0);
1270 if (wraps_update_max(wraps, &info[i]) < 0)
1271 return isl_stat_error;
1272 if (wraps_update_max(wraps, &info[j]) < 0)
1273 return isl_stat_error;
1275 return isl_stat_ok;
1278 /* Free the contents of the isl_wraps data structure.
1280 static void wraps_free(struct isl_wraps *wraps)
1282 isl_mat_free(wraps->mat);
1283 if (wraps->bound)
1284 isl_int_clear(wraps->max);
1287 /* Mark the wrapping as failed by resetting wraps->mat->n_row to zero.
1289 static isl_stat wraps_mark_failed(struct isl_wraps *wraps)
1291 wraps->mat->n_row = 0;
1292 return isl_stat_ok;
1295 /* Is the wrapping constraint in row "row" allowed?
1297 * If wraps->bound is set, we check that none of the coefficients
1298 * is greater than wraps->max.
1300 static int allow_wrap(struct isl_wraps *wraps, int row)
1302 int i;
1304 if (!wraps->bound)
1305 return 1;
1307 for (i = 1; i < wraps->mat->n_col; ++i)
1308 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1309 return 0;
1311 return 1;
1314 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1315 * to include "set" and add the result in position "w" of "wraps".
1316 * "len" is the total number of coefficients in "bound" and "ineq".
1317 * Return 1 on success, 0 on failure and -1 on error.
1318 * Wrapping can fail if the result of wrapping is equal to "bound"
1319 * or if we want to bound the sizes of the coefficients and
1320 * the wrapped constraint does not satisfy this bound.
1322 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1323 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1325 isl_seq_cpy(wraps->mat->row[w], bound, len);
1326 if (negate) {
1327 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1328 ineq = wraps->mat->row[w + 1];
1330 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1331 return -1;
1332 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1333 return 0;
1334 if (!allow_wrap(wraps, w))
1335 return 0;
1336 return 1;
1339 /* For each constraint in info->bmap that is not redundant (as determined
1340 * by info->tab) and that is not a valid constraint for the other basic map,
1341 * wrap the constraint around "bound" such that it includes the whole
1342 * set "set" and append the resulting constraint to "wraps".
1343 * Note that the constraints that are valid for the other basic map
1344 * will be added to the combined basic map by default, so there is
1345 * no need to wrap them.
1346 * The caller wrap_in_facets even relies on this function not wrapping
1347 * any constraints that are already valid.
1348 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1349 * wraps->n_row is the number of actual wrapped constraints that have
1350 * been added.
1351 * If any of the wrapping problems results in a constraint that is
1352 * identical to "bound", then this means that "set" is unbounded in such
1353 * way that no wrapping is possible. If this happens then wraps->n_row
1354 * is reset to zero.
1355 * Similarly, if we want to bound the coefficients of the wrapping
1356 * constraints and a newly added wrapping constraint does not
1357 * satisfy the bound, then wraps->n_row is also reset to zero.
1359 static isl_stat add_wraps(struct isl_wraps *wraps,
1360 struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
1362 int l, m;
1363 int w;
1364 int added;
1365 isl_basic_map *bmap = info->bmap;
1366 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1367 unsigned len = 1 + total;
1369 if (total < 0)
1370 return isl_stat_error;
1372 w = wraps->mat->n_row;
1374 for (l = 0; l < bmap->n_ineq; ++l) {
1375 if (info->ineq[l] == STATUS_VALID ||
1376 info->ineq[l] == STATUS_REDUNDANT)
1377 continue;
1378 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1379 continue;
1380 if (isl_seq_eq(bound, bmap->ineq[l], len))
1381 continue;
1382 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1383 continue;
1385 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1386 if (added < 0)
1387 return isl_stat_error;
1388 if (!added)
1389 goto unbounded;
1390 ++w;
1392 for (l = 0; l < bmap->n_eq; ++l) {
1393 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1394 continue;
1395 if (isl_seq_eq(bound, bmap->eq[l], len))
1396 continue;
1398 for (m = 0; m < 2; ++m) {
1399 if (info->eq[2 * l + m] == STATUS_VALID)
1400 continue;
1401 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1402 set, !m);
1403 if (added < 0)
1404 return isl_stat_error;
1405 if (!added)
1406 goto unbounded;
1407 ++w;
1411 wraps->mat->n_row = w;
1412 return isl_stat_ok;
1413 unbounded:
1414 return wraps_mark_failed(wraps);
1417 /* Check if the constraints in "wraps" from "first" until the last
1418 * are all valid for the basic set represented by "tab".
1419 * If not, wraps->n_row is set to zero.
1421 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1422 struct isl_tab *tab)
1424 int i;
1426 for (i = first; i < wraps->n_row; ++i) {
1427 enum isl_ineq_type type;
1428 type = isl_tab_ineq_type(tab, wraps->row[i]);
1429 if (type == isl_ineq_error)
1430 return -1;
1431 if (type == isl_ineq_redundant)
1432 continue;
1433 wraps->n_row = 0;
1434 return 0;
1437 return 0;
1440 /* Return a set that corresponds to the non-redundant constraints
1441 * (as recorded in tab) of bmap.
1443 * It's important to remove the redundant constraints as some
1444 * of the other constraints may have been modified after the
1445 * constraints were marked redundant.
1446 * In particular, a constraint may have been relaxed.
1447 * Redundant constraints are ignored when a constraint is relaxed
1448 * and should therefore continue to be ignored ever after.
1449 * Otherwise, the relaxation might be thwarted by some of
1450 * these constraints.
1452 * Update the underlying set to ensure that the dimension doesn't change.
1453 * Otherwise the integer divisions could get dropped if the tab
1454 * turns out to be empty.
1456 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1457 struct isl_tab *tab)
1459 isl_basic_set *bset;
1461 bmap = isl_basic_map_copy(bmap);
1462 bset = isl_basic_map_underlying_set(bmap);
1463 bset = isl_basic_set_cow(bset);
1464 bset = isl_basic_set_update_from_tab(bset, tab);
1465 return isl_set_from_basic_set(bset);
1468 /* Does "info" have any cut constraints that are redundant?
1470 static isl_bool has_redundant_cuts(struct isl_coalesce_info *info)
1472 int l;
1473 int n_eq, n_ineq;
1475 n_eq = isl_basic_map_n_equality(info->bmap);
1476 n_ineq = isl_basic_map_n_inequality(info->bmap);
1477 if (n_eq < 0 || n_ineq < 0)
1478 return isl_bool_error;
1479 for (l = 0; l < n_ineq; ++l) {
1480 int red;
1482 if (info->ineq[l] != STATUS_CUT)
1483 continue;
1484 red = isl_tab_is_redundant(info->tab, n_eq + l);
1485 if (red < 0)
1486 return isl_bool_error;
1487 if (red)
1488 return isl_bool_true;
1491 return isl_bool_false;
1494 /* Wrap the constraints of info->bmap that bound the facet defined
1495 * by inequality "k" around (the opposite of) this inequality to
1496 * include "set". "bound" may be used to store the negated inequality.
1497 * Since the wrapped constraints are not guaranteed to contain the whole
1498 * of info->bmap, we check them in check_wraps.
1499 * If any of the wrapped constraints turn out to be invalid, then
1500 * check_wraps will reset wrap->n_row to zero.
1502 * If any of the cut constraints of info->bmap turn out
1503 * to be redundant with respect to other constraints
1504 * then these will neither be wrapped nor added directly to the result.
1505 * The result may therefore not be correct.
1506 * Skip wrapping and reset wrap->mat->n_row to zero in this case.
1508 static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
1509 struct isl_coalesce_info *info, int k, isl_int *bound,
1510 __isl_keep isl_set *set)
1512 isl_bool nowrap;
1513 struct isl_tab_undo *snap;
1514 int n;
1515 isl_size total = isl_basic_map_dim(info->bmap, isl_dim_all);
1517 if (total < 0)
1518 return isl_stat_error;
1520 snap = isl_tab_snap(info->tab);
1522 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1523 return isl_stat_error;
1524 if (isl_tab_detect_redundant(info->tab) < 0)
1525 return isl_stat_error;
1526 nowrap = has_redundant_cuts(info);
1527 if (nowrap < 0)
1528 return isl_stat_error;
1530 n = wraps->mat->n_row;
1531 if (!nowrap) {
1532 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1534 if (add_wraps(wraps, info, bound, set) < 0)
1535 return isl_stat_error;
1538 if (isl_tab_rollback(info->tab, snap) < 0)
1539 return isl_stat_error;
1540 if (nowrap)
1541 return wraps_mark_failed(wraps);
1542 if (check_wraps(wraps->mat, n, info->tab) < 0)
1543 return isl_stat_error;
1545 return isl_stat_ok;
1548 /* Given a basic set i with a constraint k that is adjacent to
1549 * basic set j, check if we can wrap
1550 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1551 * (always) around their ridges to include the other set.
1552 * If so, replace the pair of basic sets by their union.
1554 * All constraints of i (except k) are assumed to be valid or
1555 * cut constraints for j.
1556 * Wrapping the cut constraints to include basic map j may result
1557 * in constraints that are no longer valid of basic map i
1558 * we have to check that the resulting wrapping constraints are valid for i.
1559 * If "wrap_facet" is not set, then all constraints of i (except k)
1560 * are assumed to be valid for j.
1561 * ____ _____
1562 * / | / \
1563 * / || / |
1564 * \ || => \ |
1565 * \ || \ |
1566 * \___|| \____|
1569 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1570 struct isl_coalesce_info *info, int wrap_facet)
1572 enum isl_change change = isl_change_none;
1573 struct isl_wraps wraps;
1574 isl_ctx *ctx;
1575 isl_mat *mat;
1576 struct isl_set *set_i = NULL;
1577 struct isl_set *set_j = NULL;
1578 struct isl_vec *bound = NULL;
1579 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1581 if (total < 0)
1582 return isl_change_error;
1583 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1584 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1585 ctx = isl_basic_map_get_ctx(info[i].bmap);
1586 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1587 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1588 1 + total);
1589 if (wraps_init(&wraps, mat, info, i, j) < 0)
1590 goto error;
1591 bound = isl_vec_alloc(ctx, 1 + total);
1592 if (!set_i || !set_j || !bound)
1593 goto error;
1595 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1596 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1597 isl_seq_normalize(ctx, bound->el, 1 + total);
1599 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1600 wraps.mat->n_row = 1;
1602 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1603 goto error;
1604 if (!wraps.mat->n_row)
1605 goto unbounded;
1607 if (wrap_facet) {
1608 if (add_wraps_around_facet(&wraps, &info[i], k,
1609 bound->el, set_j) < 0)
1610 goto error;
1611 if (!wraps.mat->n_row)
1612 goto unbounded;
1615 change = fuse(i, j, info, wraps.mat, 0, 0);
1617 unbounded:
1618 wraps_free(&wraps);
1620 isl_set_free(set_i);
1621 isl_set_free(set_j);
1623 isl_vec_free(bound);
1625 return change;
1626 error:
1627 wraps_free(&wraps);
1628 isl_vec_free(bound);
1629 isl_set_free(set_i);
1630 isl_set_free(set_j);
1631 return isl_change_error;
1634 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1635 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1636 * add wrapping constraints to wrap.mat for all constraints
1637 * of basic map j that bound the part of basic map j that sticks out
1638 * of the cut constraint.
1639 * "set_i" is the underlying set of basic map i.
1640 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1642 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1643 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1644 * (with respect to the integer points), so we add t(x) >= 0 instead.
1645 * Otherwise, we wrap the constraints of basic map j that are not
1646 * redundant in this intersection and that are not already valid
1647 * for basic map i over basic map i.
1648 * Note that it is sufficient to wrap the constraints to include
1649 * basic map i, because we will only wrap the constraints that do
1650 * not include basic map i already. The wrapped constraint will
1651 * therefore be more relaxed compared to the original constraint.
1652 * Since the original constraint is valid for basic map j, so is
1653 * the wrapped constraint.
1655 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1656 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1657 struct isl_tab_undo *snap)
1659 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1660 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1661 return isl_stat_error;
1662 if (isl_tab_detect_redundant(info_j->tab) < 0)
1663 return isl_stat_error;
1665 if (info_j->tab->empty)
1666 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1667 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1668 return isl_stat_error;
1670 if (isl_tab_rollback(info_j->tab, snap) < 0)
1671 return isl_stat_error;
1673 return isl_stat_ok;
1676 /* Given a pair of basic maps i and j such that j sticks out
1677 * of i at n cut constraints, each time by at most one,
1678 * try to compute wrapping constraints and replace the two
1679 * basic maps by a single basic map.
1680 * The other constraints of i are assumed to be valid for j.
1681 * "set_i" is the underlying set of basic map i.
1682 * "wraps" has been initialized to be of the right size.
1684 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1685 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1686 * of basic map j that bound the part of basic map j that sticks out
1687 * of the cut constraint.
1689 * If any wrapping fails, i.e., if we cannot wrap to touch
1690 * the union, then we give up.
1691 * Otherwise, the pair of basic maps is replaced by their union.
1693 static enum isl_change try_wrap_in_facets(int i, int j,
1694 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1695 __isl_keep isl_set *set_i)
1697 int k, l, w;
1698 isl_size total;
1699 struct isl_tab_undo *snap;
1701 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1702 if (total < 0)
1703 return isl_change_error;
1705 snap = isl_tab_snap(info[j].tab);
1707 wraps->mat->n_row = 0;
1709 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1710 for (l = 0; l < 2; ++l) {
1711 if (info[i].eq[2 * k + l] != STATUS_CUT)
1712 continue;
1713 w = wraps->mat->n_row++;
1714 if (l == 0)
1715 isl_seq_neg(wraps->mat->row[w],
1716 info[i].bmap->eq[k], 1 + total);
1717 else
1718 isl_seq_cpy(wraps->mat->row[w],
1719 info[i].bmap->eq[k], 1 + total);
1720 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1721 return isl_change_error;
1723 if (!wraps->mat->n_row)
1724 return isl_change_none;
1728 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1729 if (info[i].ineq[k] != STATUS_CUT)
1730 continue;
1731 w = wraps->mat->n_row++;
1732 isl_seq_cpy(wraps->mat->row[w],
1733 info[i].bmap->ineq[k], 1 + total);
1734 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1735 return isl_change_error;
1737 if (!wraps->mat->n_row)
1738 return isl_change_none;
1741 return fuse(i, j, info, wraps->mat, 0, 1);
1744 /* Given a pair of basic maps i and j such that j sticks out
1745 * of i at n cut constraints, each time by at most one,
1746 * try to compute wrapping constraints and replace the two
1747 * basic maps by a single basic map.
1748 * The other constraints of i are assumed to be valid for j.
1750 * The core computation is performed by try_wrap_in_facets.
1751 * This function simply extracts an underlying set representation
1752 * of basic map i and initializes the data structure for keeping
1753 * track of wrapping constraints.
1755 static enum isl_change wrap_in_facets(int i, int j, int n,
1756 struct isl_coalesce_info *info)
1758 enum isl_change change = isl_change_none;
1759 struct isl_wraps wraps;
1760 isl_ctx *ctx;
1761 isl_mat *mat;
1762 isl_set *set_i = NULL;
1763 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1764 int max_wrap;
1766 if (total < 0)
1767 return isl_change_error;
1768 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1769 return isl_change_error;
1771 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1772 max_wrap *= n;
1774 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1775 ctx = isl_basic_map_get_ctx(info[i].bmap);
1776 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1777 if (wraps_init(&wraps, mat, info, i, j) < 0)
1778 goto error;
1779 if (!set_i)
1780 goto error;
1782 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1784 wraps_free(&wraps);
1785 isl_set_free(set_i);
1787 return change;
1788 error:
1789 wraps_free(&wraps);
1790 isl_set_free(set_i);
1791 return isl_change_error;
1794 /* Return the effect of inequality "ineq" on the tableau "tab",
1795 * after relaxing the constant term of "ineq" by one.
1797 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1799 enum isl_ineq_type type;
1801 isl_int_add_ui(ineq[0], ineq[0], 1);
1802 type = isl_tab_ineq_type(tab, ineq);
1803 isl_int_sub_ui(ineq[0], ineq[0], 1);
1805 return type;
1808 /* Given two basic sets i and j,
1809 * check if relaxing all the cut constraints of i by one turns
1810 * them into valid constraint for j and check if we can wrap in
1811 * the bits that are sticking out.
1812 * If so, replace the pair by their union.
1814 * We first check if all relaxed cut inequalities of i are valid for j
1815 * and then try to wrap in the intersections of the relaxed cut inequalities
1816 * with j.
1818 * During this wrapping, we consider the points of j that lie at a distance
1819 * of exactly 1 from i. In particular, we ignore the points that lie in
1820 * between this lower-dimensional space and the basic map i.
1821 * We can therefore only apply this to integer maps.
1822 * ____ _____
1823 * / ___|_ / \
1824 * / | | / |
1825 * \ | | => \ |
1826 * \|____| \ |
1827 * \___| \____/
1829 * _____ ______
1830 * | ____|_ | \
1831 * | | | | |
1832 * | | | => | |
1833 * |_| | | |
1834 * |_____| \______|
1836 * _______
1837 * | |
1838 * | |\ |
1839 * | | \ |
1840 * | | \ |
1841 * | | \|
1842 * | | \
1843 * | |_____\
1844 * | |
1845 * |_______|
1847 * Wrapping can fail if the result of wrapping one of the facets
1848 * around its edges does not produce any new facet constraint.
1849 * In particular, this happens when we try to wrap in unbounded sets.
1851 * _______________________________________________________________________
1853 * | ___
1854 * | | |
1855 * |_| |_________________________________________________________________
1856 * |___|
1858 * The following is not an acceptable result of coalescing the above two
1859 * sets as it includes extra integer points.
1860 * _______________________________________________________________________
1862 * |
1863 * |
1865 * \______________________________________________________________________
1867 static enum isl_change can_wrap_in_set(int i, int j,
1868 struct isl_coalesce_info *info)
1870 int k, l;
1871 int n;
1872 isl_size total;
1874 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1875 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1876 return isl_change_none;
1878 n = count_eq(&info[i], STATUS_CUT) + count_ineq(&info[i], STATUS_CUT);
1879 if (n == 0)
1880 return isl_change_none;
1882 total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
1883 if (total < 0)
1884 return isl_change_error;
1885 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1886 for (l = 0; l < 2; ++l) {
1887 enum isl_ineq_type type;
1889 if (info[i].eq[2 * k + l] != STATUS_CUT)
1890 continue;
1892 if (l == 0)
1893 isl_seq_neg(info[i].bmap->eq[k],
1894 info[i].bmap->eq[k], 1 + total);
1895 type = type_of_relaxed(info[j].tab,
1896 info[i].bmap->eq[k]);
1897 if (l == 0)
1898 isl_seq_neg(info[i].bmap->eq[k],
1899 info[i].bmap->eq[k], 1 + total);
1900 if (type == isl_ineq_error)
1901 return isl_change_error;
1902 if (type != isl_ineq_redundant)
1903 return isl_change_none;
1907 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1908 enum isl_ineq_type type;
1910 if (info[i].ineq[k] != STATUS_CUT)
1911 continue;
1913 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1914 if (type == isl_ineq_error)
1915 return isl_change_error;
1916 if (type != isl_ineq_redundant)
1917 return isl_change_none;
1920 return wrap_in_facets(i, j, n, info);
1923 /* Check if either i or j has only cut constraints that can
1924 * be used to wrap in (a facet of) the other basic set.
1925 * if so, replace the pair by their union.
1927 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1929 enum isl_change change = isl_change_none;
1931 change = can_wrap_in_set(i, j, info);
1932 if (change != isl_change_none)
1933 return change;
1935 change = can_wrap_in_set(j, i, info);
1936 return change;
1939 /* Check if all inequality constraints of "i" that cut "j" cease
1940 * to be cut constraints if they are relaxed by one.
1941 * If so, collect the cut constraints in "list".
1942 * The caller is responsible for allocating "list".
1944 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1945 int *list)
1947 int l, n;
1949 n = 0;
1950 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1951 enum isl_ineq_type type;
1953 if (info[i].ineq[l] != STATUS_CUT)
1954 continue;
1955 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1956 if (type == isl_ineq_error)
1957 return isl_bool_error;
1958 if (type != isl_ineq_redundant)
1959 return isl_bool_false;
1960 list[n++] = l;
1963 return isl_bool_true;
1966 /* Given two basic maps such that "j" has at least one equality constraint
1967 * that is adjacent to an inequality constraint of "i" and such that "i" has
1968 * exactly one inequality constraint that is adjacent to an equality
1969 * constraint of "j", check whether "i" can be extended to include "j" or
1970 * whether "j" can be wrapped into "i".
1971 * All remaining constraints of "i" and "j" are assumed to be valid
1972 * or cut constraints of the other basic map.
1973 * However, none of the equality constraints of "i" are cut constraints.
1975 * If "i" has any "cut" inequality constraints, then check if relaxing
1976 * each of them by one is sufficient for them to become valid.
1977 * If so, check if the inequality constraint adjacent to an equality
1978 * constraint of "j" along with all these cut constraints
1979 * can be relaxed by one to contain exactly "j".
1980 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1982 static enum isl_change check_single_adj_eq(int i, int j,
1983 struct isl_coalesce_info *info)
1985 enum isl_change change = isl_change_none;
1986 int k;
1987 int n_cut;
1988 int *relax;
1989 isl_ctx *ctx;
1990 isl_bool try_relax;
1992 n_cut = count_ineq(&info[i], STATUS_CUT);
1994 k = find_ineq(&info[i], STATUS_ADJ_EQ);
1996 if (n_cut > 0) {
1997 ctx = isl_basic_map_get_ctx(info[i].bmap);
1998 relax = isl_calloc_array(ctx, int, 1 + n_cut);
1999 if (!relax)
2000 return isl_change_error;
2001 relax[0] = k;
2002 try_relax = all_cut_by_one(i, j, info, relax + 1);
2003 if (try_relax < 0)
2004 change = isl_change_error;
2005 } else {
2006 try_relax = isl_bool_true;
2007 relax = &k;
2009 if (try_relax && change == isl_change_none)
2010 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
2011 if (n_cut > 0)
2012 free(relax);
2013 if (change != isl_change_none)
2014 return change;
2016 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
2018 return change;
2021 /* At least one of the basic maps has an equality that is adjacent
2022 * to an inequality. Make sure that only one of the basic maps has
2023 * such an equality and that the other basic map has exactly one
2024 * inequality adjacent to an equality.
2025 * If the other basic map does not have such an inequality, then
2026 * check if all its constraints are either valid or cut constraints
2027 * and, if so, try wrapping in the first map into the second.
2028 * Otherwise, try to extend one basic map with the other or
2029 * wrap one basic map in the other.
2031 static enum isl_change check_adj_eq(int i, int j,
2032 struct isl_coalesce_info *info)
2034 if (any_eq(&info[i], STATUS_ADJ_INEQ) &&
2035 any_eq(&info[j], STATUS_ADJ_INEQ))
2036 /* ADJ EQ TOO MANY */
2037 return isl_change_none;
2039 if (any_eq(&info[i], STATUS_ADJ_INEQ))
2040 return check_adj_eq(j, i, info);
2042 /* j has an equality adjacent to an inequality in i */
2044 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1) {
2045 if (all_valid_or_cut(&info[i]))
2046 return can_wrap_in_set(i, j, info);
2047 return isl_change_none;
2049 if (any_eq(&info[i], STATUS_CUT))
2050 return isl_change_none;
2051 if (any_ineq(&info[j], STATUS_ADJ_EQ) ||
2052 any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2053 any_ineq(&info[j], STATUS_ADJ_INEQ))
2054 /* ADJ EQ TOO MANY */
2055 return isl_change_none;
2057 return check_single_adj_eq(i, j, info);
2060 /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
2061 * In particular, disjunct "i" has an inequality constraint that is adjacent
2062 * to a (combination of) equality constraint(s) of disjunct "j",
2063 * but disjunct "j" has no explicit equality constraint adjacent
2064 * to an inequality constraint of disjunct "i".
2066 * Disjunct "i" is already known not to have any equality constraints
2067 * that are adjacent to an equality or inequality constraint.
2068 * Check that, other than the inequality constraint mentioned above,
2069 * all other constraints of disjunct "i" are valid for disjunct "j".
2070 * If so, try and wrap in disjunct "j".
2072 static enum isl_change check_ineq_adj_eq(int i, int j,
2073 struct isl_coalesce_info *info)
2075 int k;
2077 if (any_eq(&info[i], STATUS_CUT))
2078 return isl_change_none;
2079 if (any_ineq(&info[i], STATUS_CUT))
2080 return isl_change_none;
2081 if (any_ineq(&info[i], STATUS_ADJ_INEQ))
2082 return isl_change_none;
2083 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1)
2084 return isl_change_none;
2086 k = find_ineq(&info[i], STATUS_ADJ_EQ);
2088 return can_wrap_in_facet(i, j, k, info, 0);
2091 /* The two basic maps lie on adjacent hyperplanes. In particular,
2092 * basic map "i" has an equality that lies parallel to basic map "j".
2093 * Check if we can wrap the facets around the parallel hyperplanes
2094 * to include the other set.
2096 * We perform basically the same operations as can_wrap_in_facet,
2097 * except that we don't need to select a facet of one of the sets.
2099 * \\ \\
2100 * \\ => \\
2101 * \ \|
2103 * If there is more than one equality of "i" adjacent to an equality of "j",
2104 * then the result will satisfy one or more equalities that are a linear
2105 * combination of these equalities. These will be encoded as pairs
2106 * of inequalities in the wrapping constraints and need to be made
2107 * explicit.
2109 static enum isl_change check_eq_adj_eq(int i, int j,
2110 struct isl_coalesce_info *info)
2112 int k;
2113 enum isl_change change = isl_change_none;
2114 int detect_equalities = 0;
2115 struct isl_wraps wraps;
2116 isl_ctx *ctx;
2117 isl_mat *mat;
2118 struct isl_set *set_i = NULL;
2119 struct isl_set *set_j = NULL;
2120 struct isl_vec *bound = NULL;
2121 isl_size total = isl_basic_map_dim(info[i].bmap, isl_dim_all);
2123 if (total < 0)
2124 return isl_change_error;
2125 if (count_eq(&info[i], STATUS_ADJ_EQ) != 1)
2126 detect_equalities = 1;
2128 k = find_eq(&info[i], STATUS_ADJ_EQ);
2130 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
2131 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
2132 ctx = isl_basic_map_get_ctx(info[i].bmap);
2133 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
2134 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
2135 1 + total);
2136 if (wraps_init(&wraps, mat, info, i, j) < 0)
2137 goto error;
2138 bound = isl_vec_alloc(ctx, 1 + total);
2139 if (!set_i || !set_j || !bound)
2140 goto error;
2142 if (k % 2 == 0)
2143 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2144 else
2145 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2146 isl_int_add_ui(bound->el[0], bound->el[0], 1);
2148 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
2149 wraps.mat->n_row = 1;
2151 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
2152 goto error;
2153 if (!wraps.mat->n_row)
2154 goto unbounded;
2156 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
2157 isl_seq_neg(bound->el, bound->el, 1 + total);
2159 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
2160 wraps.mat->n_row++;
2162 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
2163 goto error;
2164 if (!wraps.mat->n_row)
2165 goto unbounded;
2167 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
2169 if (0) {
2170 error: change = isl_change_error;
2172 unbounded:
2174 wraps_free(&wraps);
2175 isl_set_free(set_i);
2176 isl_set_free(set_j);
2177 isl_vec_free(bound);
2179 return change;
2182 /* Initialize the "eq" and "ineq" fields of "info".
2184 static void init_status(struct isl_coalesce_info *info)
2186 info->eq = info->ineq = NULL;
2189 /* Set info->eq to the positions of the equalities of info->bmap
2190 * with respect to the basic map represented by "tab".
2191 * If info->eq has already been computed, then do not compute it again.
2193 static void set_eq_status_in(struct isl_coalesce_info *info,
2194 struct isl_tab *tab)
2196 if (info->eq)
2197 return;
2198 info->eq = eq_status_in(info->bmap, tab);
2201 /* Set info->ineq to the positions of the inequalities of info->bmap
2202 * with respect to the basic map represented by "tab".
2203 * If info->ineq has already been computed, then do not compute it again.
2205 static void set_ineq_status_in(struct isl_coalesce_info *info,
2206 struct isl_tab *tab)
2208 if (info->ineq)
2209 return;
2210 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
2213 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2214 * This function assumes that init_status has been called on "info" first,
2215 * after which the "eq" and "ineq" fields may or may not have been
2216 * assigned a newly allocated array.
2218 static void clear_status(struct isl_coalesce_info *info)
2220 free(info->eq);
2221 free(info->ineq);
2224 /* Are all inequality constraints of the basic map represented by "info"
2225 * valid for the other basic map, except for a single constraint
2226 * that is adjacent to an inequality constraint of the other basic map?
2228 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2230 int i;
2231 int k = -1;
2233 for (i = 0; i < info->bmap->n_ineq; ++i) {
2234 if (info->ineq[i] == STATUS_REDUNDANT)
2235 continue;
2236 if (info->ineq[i] == STATUS_VALID)
2237 continue;
2238 if (info->ineq[i] != STATUS_ADJ_INEQ)
2239 return 0;
2240 if (k != -1)
2241 return 0;
2242 k = i;
2245 return k != -1;
2248 /* Basic map "i" has one or more equality constraints that separate it
2249 * from basic map "j". Check if it happens to be an extension
2250 * of basic map "j".
2251 * In particular, check that all constraints of "j" are valid for "i",
2252 * except for one inequality constraint that is adjacent
2253 * to an inequality constraints of "i".
2254 * If so, check for "i" being an extension of "j" by calling
2255 * is_adj_ineq_extension.
2257 * Clean up the memory allocated for keeping track of the status
2258 * of the constraints before returning.
2260 static enum isl_change separating_equality(int i, int j,
2261 struct isl_coalesce_info *info)
2263 enum isl_change change = isl_change_none;
2265 if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2266 all_ineq_valid_or_single_adj_ineq(&info[j]))
2267 change = is_adj_ineq_extension(j, i, info);
2269 clear_status(&info[i]);
2270 clear_status(&info[j]);
2271 return change;
2274 /* Check if the union of the given pair of basic maps
2275 * can be represented by a single basic map.
2276 * If so, replace the pair by the single basic map and return
2277 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2278 * Otherwise, return isl_change_none.
2279 * The two basic maps are assumed to live in the same local space.
2280 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2281 * to have been initialized by the caller, either to NULL or
2282 * to valid information.
2284 * We first check the effect of each constraint of one basic map
2285 * on the other basic map.
2286 * The constraint may be
2287 * redundant the constraint is redundant in its own
2288 * basic map and should be ignore and removed
2289 * in the end
2290 * valid all (integer) points of the other basic map
2291 * satisfy the constraint
2292 * separate no (integer) point of the other basic map
2293 * satisfies the constraint
2294 * cut some but not all points of the other basic map
2295 * satisfy the constraint
2296 * adj_eq the given constraint is adjacent (on the outside)
2297 * to an equality of the other basic map
2298 * adj_ineq the given constraint is adjacent (on the outside)
2299 * to an inequality of the other basic map
2301 * We consider seven cases in which we can replace the pair by a single
2302 * basic map. We ignore all "redundant" constraints.
2304 * 1. all constraints of one basic map are valid
2305 * => the other basic map is a subset and can be removed
2307 * 2. all constraints of both basic maps are either "valid" or "cut"
2308 * and the facets corresponding to the "cut" constraints
2309 * of one of the basic maps lies entirely inside the other basic map
2310 * => the pair can be replaced by a basic map consisting
2311 * of the valid constraints in both basic maps
2313 * 3. there is a single pair of adjacent inequalities
2314 * (all other constraints are "valid")
2315 * => the pair can be replaced by a basic map consisting
2316 * of the valid constraints in both basic maps
2318 * 4. one basic map has a single adjacent inequality, while the other
2319 * constraints are "valid". The other basic map has some
2320 * "cut" constraints, but replacing the adjacent inequality by
2321 * its opposite and adding the valid constraints of the other
2322 * basic map results in a subset of the other basic map
2323 * => the pair can be replaced by a basic map consisting
2324 * of the valid constraints in both basic maps
2326 * 5. there is a single adjacent pair of an inequality and an equality,
2327 * the other constraints of the basic map containing the inequality are
2328 * "valid". Moreover, if the inequality the basic map is relaxed
2329 * and then turned into an equality, then resulting facet lies
2330 * entirely inside the other basic map
2331 * => the pair can be replaced by the basic map containing
2332 * the inequality, with the inequality relaxed.
2334 * 6. there is a single inequality adjacent to an equality,
2335 * the other constraints of the basic map containing the inequality are
2336 * "valid". Moreover, the facets corresponding to both
2337 * the inequality and the equality can be wrapped around their
2338 * ridges to include the other basic map
2339 * => the pair can be replaced by a basic map consisting
2340 * of the valid constraints in both basic maps together
2341 * with all wrapping constraints
2343 * 7. one of the basic maps extends beyond the other by at most one.
2344 * Moreover, the facets corresponding to the cut constraints and
2345 * the pieces of the other basic map at offset one from these cut
2346 * constraints can be wrapped around their ridges to include
2347 * the union of the two basic maps
2348 * => the pair can be replaced by a basic map consisting
2349 * of the valid constraints in both basic maps together
2350 * with all wrapping constraints
2352 * 8. the two basic maps live in adjacent hyperplanes. In principle
2353 * such sets can always be combined through wrapping, but we impose
2354 * that there is only one such pair, to avoid overeager coalescing.
2356 * Throughout the computation, we maintain a collection of tableaus
2357 * corresponding to the basic maps. When the basic maps are dropped
2358 * or combined, the tableaus are modified accordingly.
2360 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2361 struct isl_coalesce_info *info)
2363 enum isl_change change = isl_change_none;
2365 set_ineq_status_in(&info[i], info[j].tab);
2366 if (info[i].bmap->n_ineq && !info[i].ineq)
2367 goto error;
2368 if (any_ineq(&info[i], STATUS_ERROR))
2369 goto error;
2370 if (any_ineq(&info[i], STATUS_SEPARATE))
2371 goto done;
2373 set_ineq_status_in(&info[j], info[i].tab);
2374 if (info[j].bmap->n_ineq && !info[j].ineq)
2375 goto error;
2376 if (any_ineq(&info[j], STATUS_ERROR))
2377 goto error;
2378 if (any_ineq(&info[j], STATUS_SEPARATE))
2379 goto done;
2381 set_eq_status_in(&info[i], info[j].tab);
2382 if (info[i].bmap->n_eq && !info[i].eq)
2383 goto error;
2384 if (any_eq(&info[i], STATUS_ERROR))
2385 goto error;
2387 set_eq_status_in(&info[j], info[i].tab);
2388 if (info[j].bmap->n_eq && !info[j].eq)
2389 goto error;
2390 if (any_eq(&info[j], STATUS_ERROR))
2391 goto error;
2393 if (any_eq(&info[i], STATUS_SEPARATE))
2394 return separating_equality(i, j, info);
2395 if (any_eq(&info[j], STATUS_SEPARATE))
2396 return separating_equality(j, i, info);
2398 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2399 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2400 drop(&info[j]);
2401 change = isl_change_drop_second;
2402 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2403 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2404 drop(&info[i]);
2405 change = isl_change_drop_first;
2406 } else if (any_eq(&info[i], STATUS_ADJ_EQ)) {
2407 change = check_eq_adj_eq(i, j, info);
2408 } else if (any_eq(&info[j], STATUS_ADJ_EQ)) {
2409 change = check_eq_adj_eq(j, i, info);
2410 } else if (any_eq(&info[i], STATUS_ADJ_INEQ) ||
2411 any_eq(&info[j], STATUS_ADJ_INEQ)) {
2412 change = check_adj_eq(i, j, info);
2413 } else if (any_ineq(&info[i], STATUS_ADJ_EQ)) {
2414 change = check_ineq_adj_eq(i, j, info);
2415 } else if (any_ineq(&info[j], STATUS_ADJ_EQ)) {
2416 change = check_ineq_adj_eq(j, i, info);
2417 } else if (any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2418 any_ineq(&info[j], STATUS_ADJ_INEQ)) {
2419 change = check_adj_ineq(i, j, info);
2420 } else {
2421 if (!any_eq(&info[i], STATUS_CUT) &&
2422 !any_eq(&info[j], STATUS_CUT))
2423 change = check_facets(i, j, info);
2424 if (change == isl_change_none)
2425 change = check_wrap(i, j, info);
2428 done:
2429 clear_status(&info[i]);
2430 clear_status(&info[j]);
2431 return change;
2432 error:
2433 clear_status(&info[i]);
2434 clear_status(&info[j]);
2435 return isl_change_error;
2438 /* Check if the union of the given pair of basic maps
2439 * can be represented by a single basic map.
2440 * If so, replace the pair by the single basic map and return
2441 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2442 * Otherwise, return isl_change_none.
2443 * The two basic maps are assumed to live in the same local space.
2445 static enum isl_change coalesce_local_pair(int i, int j,
2446 struct isl_coalesce_info *info)
2448 init_status(&info[i]);
2449 init_status(&info[j]);
2450 return coalesce_local_pair_reuse(i, j, info);
2453 /* Shift the integer division at position "div" of the basic map
2454 * represented by "info" by "shift".
2456 * That is, if the integer division has the form
2458 * floor(f(x)/d)
2460 * then replace it by
2462 * floor((f(x) + shift * d)/d) - shift
2464 static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2465 isl_int shift)
2467 isl_size total, n_div;
2469 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2470 if (!info->bmap)
2471 return isl_stat_error;
2473 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2474 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2475 if (total < 0 || n_div < 0)
2476 return isl_stat_error;
2477 total -= n_div;
2478 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2479 return isl_stat_error;
2481 return isl_stat_ok;
2484 /* If the integer division at position "div" is defined by an equality,
2485 * i.e., a stride constraint, then change the integer division expression
2486 * to have a constant term equal to zero.
2488 * Let the equality constraint be
2490 * c + f + m a = 0
2492 * The integer division expression is then typically of the form
2494 * a = floor((-f - c')/m)
2496 * The integer division is first shifted by t = floor(c/m),
2497 * turning the equality constraint into
2499 * c - m floor(c/m) + f + m a' = 0
2501 * i.e.,
2503 * (c mod m) + f + m a' = 0
2505 * That is,
2507 * a' = (-f - (c mod m))/m = floor((-f)/m)
2509 * because a' is an integer and 0 <= (c mod m) < m.
2510 * The constant term of a' can therefore be zeroed out,
2511 * but only if the integer division expression is of the expected form.
2513 static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2515 isl_bool defined, valid;
2516 isl_stat r;
2517 isl_constraint *c;
2518 isl_int shift, stride;
2520 defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2521 div, &c);
2522 if (defined < 0)
2523 return isl_stat_error;
2524 if (!defined)
2525 return isl_stat_ok;
2526 if (!c)
2527 return isl_stat_error;
2528 valid = isl_constraint_is_div_equality(c, div);
2529 isl_int_init(shift);
2530 isl_int_init(stride);
2531 isl_constraint_get_constant(c, &shift);
2532 isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2533 isl_int_fdiv_q(shift, shift, stride);
2534 r = shift_div(info, div, shift);
2535 isl_int_clear(stride);
2536 isl_int_clear(shift);
2537 isl_constraint_free(c);
2538 if (r < 0 || valid < 0)
2539 return isl_stat_error;
2540 if (!valid)
2541 return isl_stat_ok;
2542 info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2543 info->bmap, div, 0);
2544 if (!info->bmap)
2545 return isl_stat_error;
2546 return isl_stat_ok;
2549 /* The basic maps represented by "info1" and "info2" are known
2550 * to have the same number of integer divisions.
2551 * Check if pairs of integer divisions are equal to each other
2552 * despite the fact that they differ by a rational constant.
2554 * In particular, look for any pair of integer divisions that
2555 * only differ in their constant terms.
2556 * If either of these integer divisions is defined
2557 * by stride constraints, then modify it to have a zero constant term.
2558 * If both are defined by stride constraints then in the end they will have
2559 * the same (zero) constant term.
2561 static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2562 struct isl_coalesce_info *info2)
2564 int i;
2565 isl_size n;
2567 n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2568 if (n < 0)
2569 return isl_stat_error;
2570 for (i = 0; i < n; ++i) {
2571 isl_bool known, harmonize;
2573 known = isl_basic_map_div_is_known(info1->bmap, i);
2574 if (known >= 0 && known)
2575 known = isl_basic_map_div_is_known(info2->bmap, i);
2576 if (known < 0)
2577 return isl_stat_error;
2578 if (!known)
2579 continue;
2580 harmonize = isl_basic_map_equal_div_expr_except_constant(
2581 info1->bmap, i, info2->bmap, i);
2582 if (harmonize < 0)
2583 return isl_stat_error;
2584 if (!harmonize)
2585 continue;
2586 if (normalize_stride_div(info1, i) < 0)
2587 return isl_stat_error;
2588 if (normalize_stride_div(info2, i) < 0)
2589 return isl_stat_error;
2592 return isl_stat_ok;
2595 /* If "shift" is an integer constant, then shift the integer division
2596 * at position "div" of the basic map represented by "info" by "shift".
2597 * If "shift" is not an integer constant, then do nothing.
2598 * If "shift" is equal to zero, then no shift needs to be performed either.
2600 * That is, if the integer division has the form
2602 * floor(f(x)/d)
2604 * then replace it by
2606 * floor((f(x) + shift * d)/d) - shift
2608 static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2609 __isl_keep isl_aff *shift)
2611 isl_bool cst;
2612 isl_stat r;
2613 isl_int d;
2614 isl_val *c;
2616 cst = isl_aff_is_cst(shift);
2617 if (cst < 0 || !cst)
2618 return cst < 0 ? isl_stat_error : isl_stat_ok;
2620 c = isl_aff_get_constant_val(shift);
2621 cst = isl_val_is_int(c);
2622 if (cst >= 0 && cst)
2623 cst = isl_bool_not(isl_val_is_zero(c));
2624 if (cst < 0 || !cst) {
2625 isl_val_free(c);
2626 return cst < 0 ? isl_stat_error : isl_stat_ok;
2629 isl_int_init(d);
2630 r = isl_val_get_num_isl_int(c, &d);
2631 if (r >= 0)
2632 r = shift_div(info, div, d);
2633 isl_int_clear(d);
2635 isl_val_free(c);
2637 return r;
2640 /* Check if some of the divs in the basic map represented by "info1"
2641 * are shifts of the corresponding divs in the basic map represented
2642 * by "info2", taking into account the equality constraints "eq1" of "info1"
2643 * and "eq2" of "info2". If so, align them with those of "info2".
2644 * "info1" and "info2" are assumed to have the same number
2645 * of integer divisions.
2647 * An integer division is considered to be a shift of another integer
2648 * division if, after simplification with respect to the equality
2649 * constraints of the other basic map, one is equal to the other
2650 * plus a constant.
2652 * In particular, for each pair of integer divisions, if both are known,
2653 * have the same denominator and are not already equal to each other,
2654 * simplify each with respect to the equality constraints
2655 * of the other basic map. If the difference is an integer constant,
2656 * then move this difference outside.
2657 * That is, if, after simplification, one integer division is of the form
2659 * floor((f(x) + c_1)/d)
2661 * while the other is of the form
2663 * floor((f(x) + c_2)/d)
2665 * and n = (c_2 - c_1)/d is an integer, then replace the first
2666 * integer division by
2668 * floor((f_1(x) + c_1 + n * d)/d) - n,
2670 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2671 * after simplification with respect to the equality constraints.
2673 static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2674 struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2675 __isl_keep isl_basic_set *eq2)
2677 int i;
2678 isl_size total;
2679 isl_local_space *ls1, *ls2;
2681 total = isl_basic_map_dim(info1->bmap, isl_dim_all);
2682 if (total < 0)
2683 return isl_stat_error;
2684 ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2685 ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2686 for (i = 0; i < info1->bmap->n_div; ++i) {
2687 isl_stat r;
2688 isl_aff *div1, *div2;
2690 if (!isl_local_space_div_is_known(ls1, i) ||
2691 !isl_local_space_div_is_known(ls2, i))
2692 continue;
2693 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2694 continue;
2695 if (isl_seq_eq(info1->bmap->div[i] + 1,
2696 info2->bmap->div[i] + 1, 1 + total))
2697 continue;
2698 div1 = isl_local_space_get_div(ls1, i);
2699 div2 = isl_local_space_get_div(ls2, i);
2700 div1 = isl_aff_substitute_equalities(div1,
2701 isl_basic_set_copy(eq2));
2702 div2 = isl_aff_substitute_equalities(div2,
2703 isl_basic_set_copy(eq1));
2704 div2 = isl_aff_sub(div2, div1);
2705 r = shift_if_cst_int(info1, i, div2);
2706 isl_aff_free(div2);
2707 if (r < 0)
2708 break;
2710 isl_local_space_free(ls1);
2711 isl_local_space_free(ls2);
2713 if (i < info1->bmap->n_div)
2714 return isl_stat_error;
2715 return isl_stat_ok;
2718 /* Check if some of the divs in the basic map represented by "info1"
2719 * are shifts of the corresponding divs in the basic map represented
2720 * by "info2". If so, align them with those of "info2".
2721 * Only do this if "info1" and "info2" have the same number
2722 * of integer divisions.
2724 * An integer division is considered to be a shift of another integer
2725 * division if, after simplification with respect to the equality
2726 * constraints of the other basic map, one is equal to the other
2727 * plus a constant.
2729 * First check if pairs of integer divisions are equal to each other
2730 * despite the fact that they differ by a rational constant.
2731 * If so, try and arrange for them to have the same constant term.
2733 * Then, extract the equality constraints and continue with
2734 * harmonize_divs_with_hulls.
2736 * If the equality constraints of both basic maps are the same,
2737 * then there is no need to perform any shifting since
2738 * the coefficients of the integer divisions should have been
2739 * reduced in the same way.
2741 static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2742 struct isl_coalesce_info *info2)
2744 isl_bool equal;
2745 isl_basic_map *bmap1, *bmap2;
2746 isl_basic_set *eq1, *eq2;
2747 isl_stat r;
2749 if (!info1->bmap || !info2->bmap)
2750 return isl_stat_error;
2752 if (info1->bmap->n_div != info2->bmap->n_div)
2753 return isl_stat_ok;
2754 if (info1->bmap->n_div == 0)
2755 return isl_stat_ok;
2757 if (harmonize_stride_divs(info1, info2) < 0)
2758 return isl_stat_error;
2760 bmap1 = isl_basic_map_copy(info1->bmap);
2761 bmap2 = isl_basic_map_copy(info2->bmap);
2762 eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2763 eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2764 equal = isl_basic_set_plain_is_equal(eq1, eq2);
2765 if (equal < 0)
2766 r = isl_stat_error;
2767 else if (equal)
2768 r = isl_stat_ok;
2769 else
2770 r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2771 isl_basic_set_free(eq1);
2772 isl_basic_set_free(eq2);
2774 return r;
2777 /* Do the two basic maps live in the same local space, i.e.,
2778 * do they have the same (known) divs?
2779 * If either basic map has any unknown divs, then we can only assume
2780 * that they do not live in the same local space.
2782 static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
2783 __isl_keep isl_basic_map *bmap2)
2785 int i;
2786 isl_bool known;
2787 isl_size total;
2789 if (!bmap1 || !bmap2)
2790 return isl_bool_error;
2791 if (bmap1->n_div != bmap2->n_div)
2792 return isl_bool_false;
2794 if (bmap1->n_div == 0)
2795 return isl_bool_true;
2797 known = isl_basic_map_divs_known(bmap1);
2798 if (known < 0 || !known)
2799 return known;
2800 known = isl_basic_map_divs_known(bmap2);
2801 if (known < 0 || !known)
2802 return known;
2804 total = isl_basic_map_dim(bmap1, isl_dim_all);
2805 if (total < 0)
2806 return isl_bool_error;
2807 for (i = 0; i < bmap1->n_div; ++i)
2808 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2809 return isl_bool_false;
2811 return isl_bool_true;
2814 /* Assuming that "tab" contains the equality constraints and
2815 * the initial inequality constraints of "bmap", copy the remaining
2816 * inequality constraints of "bmap" to "Tab".
2818 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2820 int i, n_ineq;
2822 if (!bmap)
2823 return isl_stat_error;
2825 n_ineq = tab->n_con - tab->n_eq;
2826 for (i = n_ineq; i < bmap->n_ineq; ++i)
2827 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2828 return isl_stat_error;
2830 return isl_stat_ok;
2833 /* Description of an integer division that is added
2834 * during an expansion.
2835 * "pos" is the position of the corresponding variable.
2836 * "cst" indicates whether this integer division has a fixed value.
2837 * "val" contains the fixed value, if the value is fixed.
2839 struct isl_expanded {
2840 int pos;
2841 isl_bool cst;
2842 isl_int val;
2845 /* For each of the "n" integer division variables "expanded",
2846 * if the variable has a fixed value, then add two inequality
2847 * constraints expressing the fixed value.
2848 * Otherwise, add the corresponding div constraints.
2849 * The caller is responsible for removing the div constraints
2850 * that it added for all these "n" integer divisions.
2852 * The div constraints and the pair of inequality constraints
2853 * forcing the fixed value cannot both be added for a given variable
2854 * as the combination may render some of the original constraints redundant.
2855 * These would then be ignored during the coalescing detection,
2856 * while they could remain in the fused result.
2858 * The two added inequality constraints are
2860 * -a + v >= 0
2861 * a - v >= 0
2863 * with "a" the variable and "v" its fixed value.
2864 * The facet corresponding to one of these two constraints is selected
2865 * in the tableau to ensure that the pair of inequality constraints
2866 * is treated as an equality constraint.
2868 * The information in info->ineq is thrown away because it was
2869 * computed in terms of div constraints, while some of those
2870 * have now been replaced by these pairs of inequality constraints.
2872 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2873 int n, struct isl_expanded *expanded)
2875 unsigned o_div;
2876 int i;
2877 isl_vec *ineq;
2879 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2880 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2881 if (!ineq)
2882 return isl_stat_error;
2883 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2885 for (i = 0; i < n; ++i) {
2886 if (!expanded[i].cst) {
2887 info->bmap = isl_basic_map_extend_constraints(
2888 info->bmap, 0, 2);
2889 info->bmap = isl_basic_map_add_div_constraints(
2890 info->bmap, expanded[i].pos - o_div);
2891 } else {
2892 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2893 isl_int_set(ineq->el[0], expanded[i].val);
2894 info->bmap = isl_basic_map_add_ineq(info->bmap,
2895 ineq->el);
2896 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2897 isl_int_neg(ineq->el[0], expanded[i].val);
2898 info->bmap = isl_basic_map_add_ineq(info->bmap,
2899 ineq->el);
2900 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2902 if (copy_ineq(info->tab, info->bmap) < 0)
2903 break;
2904 if (expanded[i].cst &&
2905 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2906 break;
2909 isl_vec_free(ineq);
2911 clear_status(info);
2912 init_status(info);
2914 return i < n ? isl_stat_error : isl_stat_ok;
2917 /* Insert the "n" integer division variables "expanded"
2918 * into info->tab and info->bmap and
2919 * update info->ineq with respect to the redundant constraints
2920 * in the resulting tableau.
2921 * "bmap" contains the result of this insertion in info->bmap,
2922 * while info->bmap is the original version
2923 * of "bmap", i.e., the one that corresponds to the current
2924 * state of info->tab. The number of constraints in info->bmap
2925 * is assumed to be the same as the number of constraints
2926 * in info->tab. This is required to be able to detect
2927 * the extra constraints in "bmap".
2929 * In particular, introduce extra variables corresponding
2930 * to the extra integer divisions and add the div constraints
2931 * that were added to "bmap" after info->tab was created
2932 * from info->bmap.
2933 * Furthermore, check if these extra integer divisions happen
2934 * to attain a fixed integer value in info->tab.
2935 * If so, replace the corresponding div constraints by pairs
2936 * of inequality constraints that fix these
2937 * integer divisions to their single integer values.
2938 * Replace info->bmap by "bmap" to match the changes to info->tab.
2939 * info->ineq was computed without a tableau and therefore
2940 * does not take into account the redundant constraints
2941 * in the tableau. Mark them here.
2942 * There is no need to check the newly added div constraints
2943 * since they cannot be redundant.
2944 * The redundancy check is not performed when constants have been discovered
2945 * since info->ineq is completely thrown away in this case.
2947 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2948 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2950 int i, n_ineq;
2951 unsigned n_eq;
2952 struct isl_tab_undo *snap;
2953 int any;
2955 if (!bmap)
2956 return isl_stat_error;
2957 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2958 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2959 "original tableau does not correspond "
2960 "to original basic map", goto error);
2962 if (isl_tab_extend_vars(info->tab, n) < 0)
2963 goto error;
2964 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2965 goto error;
2967 for (i = 0; i < n; ++i) {
2968 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2969 goto error;
2972 snap = isl_tab_snap(info->tab);
2974 n_ineq = info->tab->n_con - info->tab->n_eq;
2975 if (copy_ineq(info->tab, bmap) < 0)
2976 goto error;
2978 isl_basic_map_free(info->bmap);
2979 info->bmap = bmap;
2981 any = 0;
2982 for (i = 0; i < n; ++i) {
2983 expanded[i].cst = isl_tab_is_constant(info->tab,
2984 expanded[i].pos, &expanded[i].val);
2985 if (expanded[i].cst < 0)
2986 return isl_stat_error;
2987 if (expanded[i].cst)
2988 any = 1;
2991 if (any) {
2992 if (isl_tab_rollback(info->tab, snap) < 0)
2993 return isl_stat_error;
2994 info->bmap = isl_basic_map_cow(info->bmap);
2995 info->bmap = isl_basic_map_free_inequality(info->bmap, 2 * n);
2996 if (info->bmap < 0)
2997 return isl_stat_error;
2999 return fix_constant_divs(info, n, expanded);
3002 n_eq = info->bmap->n_eq;
3003 for (i = 0; i < n_ineq; ++i) {
3004 if (isl_tab_is_redundant(info->tab, n_eq + i))
3005 info->ineq[i] = STATUS_REDUNDANT;
3008 return isl_stat_ok;
3009 error:
3010 isl_basic_map_free(bmap);
3011 return isl_stat_error;
3014 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
3015 * in isl_basic_map_expand_divs using the expansion "exp" and
3016 * update info->ineq with respect to the redundant constraints
3017 * in the resulting tableau. info->bmap is the original version
3018 * of "bmap", i.e., the one that corresponds to the current
3019 * state of info->tab. The number of constraints in info->bmap
3020 * is assumed to be the same as the number of constraints
3021 * in info->tab. This is required to be able to detect
3022 * the extra constraints in "bmap".
3024 * Extract the positions where extra local variables are introduced
3025 * from "exp" and call tab_insert_divs.
3027 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
3028 __isl_take isl_basic_map *bmap)
3030 isl_ctx *ctx;
3031 struct isl_expanded *expanded;
3032 int i, j, k, n;
3033 int extra_var;
3034 isl_size total, n_div;
3035 unsigned pos;
3036 isl_stat r;
3038 total = isl_basic_map_dim(bmap, isl_dim_all);
3039 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3040 if (total < 0 || n_div < 0)
3041 return isl_stat_error;
3042 pos = total - n_div;
3043 extra_var = total - info->tab->n_var;
3044 n = n_div - extra_var;
3046 ctx = isl_basic_map_get_ctx(bmap);
3047 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
3048 if (extra_var && !expanded)
3049 goto error;
3051 i = 0;
3052 k = 0;
3053 for (j = 0; j < n_div; ++j) {
3054 if (i < n && exp[i] == j) {
3055 ++i;
3056 continue;
3058 expanded[k++].pos = pos + j;
3061 for (k = 0; k < extra_var; ++k)
3062 isl_int_init(expanded[k].val);
3064 r = tab_insert_divs(info, extra_var, expanded, bmap);
3066 for (k = 0; k < extra_var; ++k)
3067 isl_int_clear(expanded[k].val);
3068 free(expanded);
3070 return r;
3071 error:
3072 isl_basic_map_free(bmap);
3073 return isl_stat_error;
3076 /* Check if the union of the basic maps represented by info[i] and info[j]
3077 * can be represented by a single basic map,
3078 * after expanding the divs of info[i] to match those of info[j].
3079 * If so, replace the pair by the single basic map and return
3080 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3081 * Otherwise, return isl_change_none.
3083 * The caller has already checked for info[j] being a subset of info[i].
3084 * If some of the divs of info[j] are unknown, then the expanded info[i]
3085 * will not have the corresponding div constraints. The other patterns
3086 * therefore cannot apply. Skip the computation in this case.
3088 * The expansion is performed using the divs "div" and expansion "exp"
3089 * computed by the caller.
3090 * info[i].bmap has already been expanded and the result is passed in
3091 * as "bmap".
3092 * The "eq" and "ineq" fields of info[i] reflect the status of
3093 * the constraints of the expanded "bmap" with respect to info[j].tab.
3094 * However, inequality constraints that are redundant in info[i].tab
3095 * have not yet been marked as such because no tableau was available.
3097 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
3098 * updating info[i].ineq with respect to the redundant constraints.
3099 * Then try and coalesce the expanded info[i] with info[j],
3100 * reusing the information in info[i].eq and info[i].ineq.
3101 * If this does not result in any coalescing or if it results in info[j]
3102 * getting dropped (which should not happen in practice, since the case
3103 * of info[j] being a subset of info[i] has already been checked by
3104 * the caller), then revert info[i] to its original state.
3106 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
3107 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
3108 int *exp)
3110 isl_bool known;
3111 isl_basic_map *bmap_i;
3112 struct isl_tab_undo *snap;
3113 enum isl_change change = isl_change_none;
3115 known = isl_basic_map_divs_known(info[j].bmap);
3116 if (known < 0 || !known) {
3117 clear_status(&info[i]);
3118 isl_basic_map_free(bmap);
3119 return known < 0 ? isl_change_error : isl_change_none;
3122 bmap_i = isl_basic_map_copy(info[i].bmap);
3123 snap = isl_tab_snap(info[i].tab);
3124 if (expand_tab(&info[i], exp, bmap) < 0)
3125 change = isl_change_error;
3127 init_status(&info[j]);
3128 if (change == isl_change_none)
3129 change = coalesce_local_pair_reuse(i, j, info);
3130 else
3131 clear_status(&info[i]);
3132 if (change != isl_change_none && change != isl_change_drop_second) {
3133 isl_basic_map_free(bmap_i);
3134 } else {
3135 isl_basic_map_free(info[i].bmap);
3136 info[i].bmap = bmap_i;
3138 if (isl_tab_rollback(info[i].tab, snap) < 0)
3139 change = isl_change_error;
3142 return change;
3145 /* Check if the union of "bmap" and the basic map represented by info[j]
3146 * can be represented by a single basic map,
3147 * after expanding the divs of "bmap" to match those of info[j].
3148 * If so, replace the pair by the single basic map and return
3149 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3150 * Otherwise, return isl_change_none.
3152 * In particular, check if the expanded "bmap" contains the basic map
3153 * represented by the tableau info[j].tab.
3154 * The expansion is performed using the divs "div" and expansion "exp"
3155 * computed by the caller.
3156 * Then we check if all constraints of the expanded "bmap" are valid for
3157 * info[j].tab.
3159 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3160 * In this case, the positions of the constraints of info[i].bmap
3161 * with respect to the basic map represented by info[j] are stored
3162 * in info[i].
3164 * If the expanded "bmap" does not contain the basic map
3165 * represented by the tableau info[j].tab and if "i" is not -1,
3166 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
3167 * as well and check if that results in coalescing.
3169 static enum isl_change coalesce_with_expanded_divs(
3170 __isl_keep isl_basic_map *bmap, int i, int j,
3171 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
3173 enum isl_change change = isl_change_none;
3174 struct isl_coalesce_info info_local, *info_i;
3176 info_i = i >= 0 ? &info[i] : &info_local;
3177 init_status(info_i);
3178 bmap = isl_basic_map_copy(bmap);
3179 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
3180 bmap = isl_basic_map_mark_final(bmap);
3182 if (!bmap)
3183 goto error;
3185 info_local.bmap = bmap;
3186 info_i->eq = eq_status_in(bmap, info[j].tab);
3187 if (bmap->n_eq && !info_i->eq)
3188 goto error;
3189 if (any_eq(info_i, STATUS_ERROR))
3190 goto error;
3191 if (any_eq(info_i, STATUS_SEPARATE))
3192 goto done;
3194 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
3195 if (bmap->n_ineq && !info_i->ineq)
3196 goto error;
3197 if (any_ineq(info_i, STATUS_ERROR))
3198 goto error;
3199 if (any_ineq(info_i, STATUS_SEPARATE))
3200 goto done;
3202 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
3203 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
3204 drop(&info[j]);
3205 change = isl_change_drop_second;
3208 if (change == isl_change_none && i != -1)
3209 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
3211 done:
3212 isl_basic_map_free(bmap);
3213 clear_status(info_i);
3214 return change;
3215 error:
3216 isl_basic_map_free(bmap);
3217 clear_status(info_i);
3218 return isl_change_error;
3221 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3222 * can be represented by a single basic map,
3223 * after aligning the divs of "bmap_i" to match those of info[j].
3224 * If so, replace the pair by the single basic map and return
3225 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3226 * Otherwise, return isl_change_none.
3228 * In particular, check if "bmap_i" contains the basic map represented by
3229 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3230 * Note that this can only succeed if the number of divs of "bmap_i"
3231 * is smaller than (or equal to) the number of divs of info[j].
3233 * We first check if the divs of "bmap_i" are all known and form a subset
3234 * of those of info[j].bmap. If so, we pass control over to
3235 * coalesce_with_expanded_divs.
3237 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3239 static enum isl_change coalesce_after_aligning_divs(
3240 __isl_keep isl_basic_map *bmap_i, int i, int j,
3241 struct isl_coalesce_info *info)
3243 isl_bool known;
3244 isl_mat *div_i, *div_j, *div;
3245 int *exp1 = NULL;
3246 int *exp2 = NULL;
3247 isl_ctx *ctx;
3248 enum isl_change change;
3250 known = isl_basic_map_divs_known(bmap_i);
3251 if (known < 0)
3252 return isl_change_error;
3253 if (!known)
3254 return isl_change_none;
3256 ctx = isl_basic_map_get_ctx(bmap_i);
3258 div_i = isl_basic_map_get_divs(bmap_i);
3259 div_j = isl_basic_map_get_divs(info[j].bmap);
3261 if (!div_i || !div_j)
3262 goto error;
3264 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3265 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3266 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
3267 goto error;
3269 div = isl_merge_divs(div_i, div_j, exp1, exp2);
3270 if (!div)
3271 goto error;
3273 if (div->n_row == div_j->n_row)
3274 change = coalesce_with_expanded_divs(bmap_i,
3275 i, j, info, div, exp1);
3276 else
3277 change = isl_change_none;
3279 isl_mat_free(div);
3281 isl_mat_free(div_i);
3282 isl_mat_free(div_j);
3284 free(exp2);
3285 free(exp1);
3287 return change;
3288 error:
3289 isl_mat_free(div_i);
3290 isl_mat_free(div_j);
3291 free(exp1);
3292 free(exp2);
3293 return isl_change_error;
3296 /* Check if basic map "j" is a subset of basic map "i" after
3297 * exploiting the extra equalities of "j" to simplify the divs of "i".
3298 * If so, remove basic map "j" and return isl_change_drop_second.
3300 * If "j" does not have any equalities or if they are the same
3301 * as those of "i", then we cannot exploit them to simplify the divs.
3302 * Similarly, if there are no divs in "i", then they cannot be simplified.
3303 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3304 * then "j" cannot be a subset of "i".
3306 * Otherwise, we intersect "i" with the affine hull of "j" and then
3307 * check if "j" is a subset of the result after aligning the divs.
3308 * If so, then "j" is definitely a subset of "i" and can be removed.
3309 * Note that if after intersection with the affine hull of "j".
3310 * "i" still has more divs than "j", then there is no way we can
3311 * align the divs of "i" to those of "j".
3313 static enum isl_change coalesce_subset_with_equalities(int i, int j,
3314 struct isl_coalesce_info *info)
3316 isl_basic_map *hull_i, *hull_j, *bmap_i;
3317 int equal, empty;
3318 enum isl_change change;
3320 if (info[j].bmap->n_eq == 0)
3321 return isl_change_none;
3322 if (info[i].bmap->n_div == 0)
3323 return isl_change_none;
3325 hull_i = isl_basic_map_copy(info[i].bmap);
3326 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3327 hull_j = isl_basic_map_copy(info[j].bmap);
3328 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3330 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3331 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3332 empty = isl_basic_map_plain_is_empty(hull_j);
3333 isl_basic_map_free(hull_i);
3335 if (equal < 0 || equal || empty < 0 || empty) {
3336 isl_basic_map_free(hull_j);
3337 if (equal < 0 || empty < 0)
3338 return isl_change_error;
3339 return isl_change_none;
3342 bmap_i = isl_basic_map_copy(info[i].bmap);
3343 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3344 if (!bmap_i)
3345 return isl_change_error;
3347 if (bmap_i->n_div > info[j].bmap->n_div) {
3348 isl_basic_map_free(bmap_i);
3349 return isl_change_none;
3352 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3354 isl_basic_map_free(bmap_i);
3356 return change;
3359 /* Check if the union of and the basic maps represented by info[i] and info[j]
3360 * can be represented by a single basic map, by aligning or equating
3361 * their integer divisions.
3362 * If so, replace the pair by the single basic map and return
3363 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3364 * Otherwise, return isl_change_none.
3366 * Note that we only perform any test if the number of divs is different
3367 * in the two basic maps. In case the number of divs is the same,
3368 * we have already established that the divs are different
3369 * in the two basic maps.
3370 * In particular, if the number of divs of basic map i is smaller than
3371 * the number of divs of basic map j, then we check if j is a subset of i
3372 * and vice versa.
3374 static enum isl_change coalesce_divs(int i, int j,
3375 struct isl_coalesce_info *info)
3377 enum isl_change change = isl_change_none;
3379 if (info[i].bmap->n_div < info[j].bmap->n_div)
3380 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3381 if (change != isl_change_none)
3382 return change;
3384 if (info[j].bmap->n_div < info[i].bmap->n_div)
3385 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3386 if (change != isl_change_none)
3387 return invert_change(change);
3389 change = coalesce_subset_with_equalities(i, j, info);
3390 if (change != isl_change_none)
3391 return change;
3393 change = coalesce_subset_with_equalities(j, i, info);
3394 if (change != isl_change_none)
3395 return invert_change(change);
3397 return isl_change_none;
3400 /* Does "bmap" involve any divs that themselves refer to divs?
3402 static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
3404 int i;
3405 isl_size total;
3406 isl_size n_div;
3408 total = isl_basic_map_dim(bmap, isl_dim_all);
3409 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3410 if (total < 0 || n_div < 0)
3411 return isl_bool_error;
3412 total -= n_div;
3414 for (i = 0; i < n_div; ++i)
3415 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
3416 n_div) != -1)
3417 return isl_bool_true;
3419 return isl_bool_false;
3422 /* Return a list of affine expressions, one for each integer division
3423 * in "bmap_i". For each integer division that also appears in "bmap_j",
3424 * the affine expression is set to NaN. The number of NaNs in the list
3425 * is equal to the number of integer divisions in "bmap_j".
3426 * For the other integer divisions of "bmap_i", the corresponding
3427 * element in the list is a purely affine expression equal to the integer
3428 * division in "hull".
3429 * If no such list can be constructed, then the number of elements
3430 * in the returned list is smaller than the number of integer divisions
3431 * in "bmap_i".
3433 static __isl_give isl_aff_list *set_up_substitutions(
3434 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3435 __isl_take isl_basic_map *hull)
3437 isl_size n_div_i, n_div_j, total;
3438 isl_ctx *ctx;
3439 isl_local_space *ls;
3440 isl_basic_set *wrap_hull;
3441 isl_aff *aff_nan;
3442 isl_aff_list *list;
3443 int i, j;
3445 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3446 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3447 total = isl_basic_map_dim(bmap_i, isl_dim_all);
3448 if (!hull || n_div_i < 0 || n_div_j < 0 || total < 0)
3449 return NULL;
3451 ctx = isl_basic_map_get_ctx(hull);
3452 total -= n_div_i;
3454 ls = isl_basic_map_get_local_space(bmap_i);
3455 ls = isl_local_space_wrap(ls);
3456 wrap_hull = isl_basic_map_wrap(hull);
3458 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3459 list = isl_aff_list_alloc(ctx, n_div_i);
3461 j = 0;
3462 for (i = 0; i < n_div_i; ++i) {
3463 isl_aff *aff;
3464 isl_size n_div;
3466 if (j < n_div_j &&
3467 isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3468 0, 2 + total)) {
3469 ++j;
3470 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3471 continue;
3473 if (n_div_i - i <= n_div_j - j)
3474 break;
3476 aff = isl_local_space_get_div(ls, i);
3477 aff = isl_aff_substitute_equalities(aff,
3478 isl_basic_set_copy(wrap_hull));
3479 aff = isl_aff_floor(aff);
3480 n_div = isl_aff_dim(aff, isl_dim_div);
3481 if (n_div < 0)
3482 goto error;
3483 if (n_div != 0) {
3484 isl_aff_free(aff);
3485 break;
3488 list = isl_aff_list_add(list, aff);
3491 isl_aff_free(aff_nan);
3492 isl_local_space_free(ls);
3493 isl_basic_set_free(wrap_hull);
3495 return list;
3496 error:
3497 isl_aff_free(aff_nan);
3498 isl_local_space_free(ls);
3499 isl_basic_set_free(wrap_hull);
3500 isl_aff_list_free(list);
3501 return NULL;
3504 /* Add variables to info->bmap and info->tab corresponding to the elements
3505 * in "list" that are not set to NaN.
3506 * "extra_var" is the number of these elements.
3507 * "dim" is the offset in the variables of "tab" where we should
3508 * start considering the elements in "list".
3509 * When this function returns, the total number of variables in "tab"
3510 * is equal to "dim" plus the number of elements in "list".
3512 * The newly added existentially quantified variables are not given
3513 * an explicit representation because the corresponding div constraints
3514 * do not appear in info->bmap. These constraints are not added
3515 * to info->bmap because for internal consistency, they would need to
3516 * be added to info->tab as well, where they could combine with the equality
3517 * that is added later to result in constraints that do not hold
3518 * in the original input.
3520 static isl_stat add_sub_vars(struct isl_coalesce_info *info,
3521 __isl_keep isl_aff_list *list, int dim, int extra_var)
3523 int i, j, d;
3524 isl_size n;
3525 isl_space *space;
3527 space = isl_basic_map_get_space(info->bmap);
3528 info->bmap = isl_basic_map_cow(info->bmap);
3529 info->bmap = isl_basic_map_extend_space(info->bmap, space,
3530 extra_var, 0, 0);
3531 n = isl_aff_list_n_aff(list);
3532 if (!info->bmap || n < 0)
3533 return isl_stat_error;
3534 for (i = 0; i < n; ++i) {
3535 int is_nan;
3536 isl_aff *aff;
3538 aff = isl_aff_list_get_aff(list, i);
3539 is_nan = isl_aff_is_nan(aff);
3540 isl_aff_free(aff);
3541 if (is_nan < 0)
3542 return isl_stat_error;
3543 if (is_nan)
3544 continue;
3546 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3547 return isl_stat_error;
3548 d = isl_basic_map_alloc_div(info->bmap);
3549 if (d < 0)
3550 return isl_stat_error;
3551 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3552 for (j = d; j > i; --j)
3553 info->bmap = isl_basic_map_swap_div(info->bmap,
3554 j - 1, j);
3555 if (!info->bmap)
3556 return isl_stat_error;
3559 return isl_stat_ok;
3562 /* For each element in "list" that is not set to NaN, fix the corresponding
3563 * variable in "tab" to the purely affine expression defined by the element.
3564 * "dim" is the offset in the variables of "tab" where we should
3565 * start considering the elements in "list".
3567 * This function assumes that a sufficient number of rows and
3568 * elements in the constraint array are available in the tableau.
3570 static isl_stat add_sub_equalities(struct isl_tab *tab,
3571 __isl_keep isl_aff_list *list, int dim)
3573 int i;
3574 isl_size n;
3575 isl_ctx *ctx;
3576 isl_vec *sub;
3577 isl_aff *aff;
3579 n = isl_aff_list_n_aff(list);
3580 if (n < 0)
3581 return isl_stat_error;
3583 ctx = isl_tab_get_ctx(tab);
3584 sub = isl_vec_alloc(ctx, 1 + dim + n);
3585 if (!sub)
3586 return isl_stat_error;
3587 isl_seq_clr(sub->el + 1 + dim, n);
3589 for (i = 0; i < n; ++i) {
3590 aff = isl_aff_list_get_aff(list, i);
3591 if (!aff)
3592 goto error;
3593 if (isl_aff_is_nan(aff)) {
3594 isl_aff_free(aff);
3595 continue;
3597 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3598 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3599 if (isl_tab_add_eq(tab, sub->el) < 0)
3600 goto error;
3601 isl_int_set_si(sub->el[1 + dim + i], 0);
3602 isl_aff_free(aff);
3605 isl_vec_free(sub);
3606 return isl_stat_ok;
3607 error:
3608 isl_aff_free(aff);
3609 isl_vec_free(sub);
3610 return isl_stat_error;
3613 /* Add variables to info->tab and info->bmap corresponding to the elements
3614 * in "list" that are not set to NaN. The value of the added variable
3615 * in info->tab is fixed to the purely affine expression defined by the element.
3616 * "dim" is the offset in the variables of info->tab where we should
3617 * start considering the elements in "list".
3618 * When this function returns, the total number of variables in info->tab
3619 * is equal to "dim" plus the number of elements in "list".
3621 static isl_stat add_subs(struct isl_coalesce_info *info,
3622 __isl_keep isl_aff_list *list, int dim)
3624 int extra_var;
3625 isl_size n;
3627 n = isl_aff_list_n_aff(list);
3628 if (n < 0)
3629 return isl_stat_error;
3631 extra_var = n - (info->tab->n_var - dim);
3633 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3634 return isl_stat_error;
3635 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3636 return isl_stat_error;
3637 if (add_sub_vars(info, list, dim, extra_var) < 0)
3638 return isl_stat_error;
3640 return add_sub_equalities(info->tab, list, dim);
3643 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3644 * divisions in "i" but not in "j" to basic map "j", with values
3645 * specified by "list". The total number of elements in "list"
3646 * is equal to the number of integer divisions in "i", while the number
3647 * of NaN elements in the list is equal to the number of integer divisions
3648 * in "j".
3650 * If no coalescing can be performed, then we need to revert basic map "j"
3651 * to its original state. We do the same if basic map "i" gets dropped
3652 * during the coalescing, even though this should not happen in practice
3653 * since we have already checked for "j" being a subset of "i"
3654 * before we reach this stage.
3656 static enum isl_change coalesce_with_subs(int i, int j,
3657 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3659 isl_basic_map *bmap_j;
3660 struct isl_tab_undo *snap;
3661 isl_size dim, n_div;
3662 enum isl_change change;
3664 bmap_j = isl_basic_map_copy(info[j].bmap);
3665 snap = isl_tab_snap(info[j].tab);
3667 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3668 n_div = isl_basic_map_dim(bmap_j, isl_dim_div);
3669 if (dim < 0 || n_div < 0)
3670 goto error;
3671 dim -= n_div;
3672 if (add_subs(&info[j], list, dim) < 0)
3673 goto error;
3675 change = coalesce_local_pair(i, j, info);
3676 if (change != isl_change_none && change != isl_change_drop_first) {
3677 isl_basic_map_free(bmap_j);
3678 } else {
3679 isl_basic_map_free(info[j].bmap);
3680 info[j].bmap = bmap_j;
3682 if (isl_tab_rollback(info[j].tab, snap) < 0)
3683 return isl_change_error;
3686 return change;
3687 error:
3688 isl_basic_map_free(bmap_j);
3689 return isl_change_error;
3692 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3693 * those extra integer divisions in "i" that can be simplified away
3694 * using the extra equalities in "j".
3695 * All divs are assumed to be known and not contain any nested divs.
3697 * We first check if there are any extra equalities in "j" that we
3698 * can exploit. Then we check if every integer division in "i"
3699 * either already appears in "j" or can be simplified using the
3700 * extra equalities to a purely affine expression.
3701 * If these tests succeed, then we try to coalesce the two basic maps
3702 * by introducing extra dimensions in "j" corresponding to
3703 * the extra integer divisions "i" fixed to the corresponding
3704 * purely affine expression.
3706 static enum isl_change check_coalesce_into_eq(int i, int j,
3707 struct isl_coalesce_info *info)
3709 isl_size n_div_i, n_div_j, n;
3710 isl_basic_map *hull_i, *hull_j;
3711 isl_bool equal, empty;
3712 isl_aff_list *list;
3713 enum isl_change change;
3715 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3716 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3717 if (n_div_i < 0 || n_div_j < 0)
3718 return isl_change_error;
3719 if (n_div_i <= n_div_j)
3720 return isl_change_none;
3721 if (info[j].bmap->n_eq == 0)
3722 return isl_change_none;
3724 hull_i = isl_basic_map_copy(info[i].bmap);
3725 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3726 hull_j = isl_basic_map_copy(info[j].bmap);
3727 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3729 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3730 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3731 empty = isl_basic_map_plain_is_empty(hull_j);
3732 isl_basic_map_free(hull_i);
3734 if (equal < 0 || empty < 0)
3735 goto error;
3736 if (equal || empty) {
3737 isl_basic_map_free(hull_j);
3738 return isl_change_none;
3741 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3742 if (!list)
3743 return isl_change_error;
3744 n = isl_aff_list_n_aff(list);
3745 if (n < 0)
3746 change = isl_change_error;
3747 else if (n < n_div_i)
3748 change = isl_change_none;
3749 else
3750 change = coalesce_with_subs(i, j, info, list);
3752 isl_aff_list_free(list);
3754 return change;
3755 error:
3756 isl_basic_map_free(hull_j);
3757 return isl_change_error;
3760 /* Check if we can coalesce basic maps "i" and "j" after copying
3761 * those extra integer divisions in one of the basic maps that can
3762 * be simplified away using the extra equalities in the other basic map.
3763 * We require all divs to be known in both basic maps.
3764 * Furthermore, to simplify the comparison of div expressions,
3765 * we do not allow any nested integer divisions.
3767 static enum isl_change check_coalesce_eq(int i, int j,
3768 struct isl_coalesce_info *info)
3770 isl_bool known, nested;
3771 enum isl_change change;
3773 known = isl_basic_map_divs_known(info[i].bmap);
3774 if (known < 0 || !known)
3775 return known < 0 ? isl_change_error : isl_change_none;
3776 known = isl_basic_map_divs_known(info[j].bmap);
3777 if (known < 0 || !known)
3778 return known < 0 ? isl_change_error : isl_change_none;
3779 nested = has_nested_div(info[i].bmap);
3780 if (nested < 0 || nested)
3781 return nested < 0 ? isl_change_error : isl_change_none;
3782 nested = has_nested_div(info[j].bmap);
3783 if (nested < 0 || nested)
3784 return nested < 0 ? isl_change_error : isl_change_none;
3786 change = check_coalesce_into_eq(i, j, info);
3787 if (change != isl_change_none)
3788 return change;
3789 change = check_coalesce_into_eq(j, i, info);
3790 if (change != isl_change_none)
3791 return invert_change(change);
3793 return isl_change_none;
3796 /* Check if the union of the given pair of basic maps
3797 * can be represented by a single basic map.
3798 * If so, replace the pair by the single basic map and return
3799 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3800 * Otherwise, return isl_change_none.
3802 * We first check if the two basic maps live in the same local space,
3803 * after aligning the divs that differ by only an integer constant.
3804 * If so, we do the complete check. Otherwise, we check if they have
3805 * the same number of integer divisions and can be coalesced, if one is
3806 * an obvious subset of the other or if the extra integer divisions
3807 * of one basic map can be simplified away using the extra equalities
3808 * of the other basic map.
3810 * Note that trying to coalesce pairs of disjuncts with the same
3811 * number, but different local variables may drop the explicit
3812 * representation of some of these local variables.
3813 * This operation is therefore not performed when
3814 * the "coalesce_preserve_locals" option is set.
3816 static enum isl_change coalesce_pair(int i, int j,
3817 struct isl_coalesce_info *info)
3819 int preserve;
3820 isl_bool same;
3821 enum isl_change change;
3822 isl_ctx *ctx;
3824 if (harmonize_divs(&info[i], &info[j]) < 0)
3825 return isl_change_error;
3826 same = same_divs(info[i].bmap, info[j].bmap);
3827 if (same < 0)
3828 return isl_change_error;
3829 if (same)
3830 return coalesce_local_pair(i, j, info);
3832 ctx = isl_basic_map_get_ctx(info[i].bmap);
3833 preserve = isl_options_get_coalesce_preserve_locals(ctx);
3834 if (!preserve && info[i].bmap->n_div == info[j].bmap->n_div) {
3835 change = coalesce_local_pair(i, j, info);
3836 if (change != isl_change_none)
3837 return change;
3840 change = coalesce_divs(i, j, info);
3841 if (change != isl_change_none)
3842 return change;
3844 return check_coalesce_eq(i, j, info);
3847 /* Return the maximum of "a" and "b".
3849 static int isl_max(int a, int b)
3851 return a > b ? a : b;
3854 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3855 * with those in the range [start2, end2[, skipping basic maps
3856 * that have been removed (either before or within this function).
3858 * For each basic map i in the first range, we check if it can be coalesced
3859 * with respect to any previously considered basic map j in the second range.
3860 * If i gets dropped (because it was a subset of some j), then
3861 * we can move on to the next basic map.
3862 * If j gets dropped, we need to continue checking against the other
3863 * previously considered basic maps.
3864 * If the two basic maps got fused, then we recheck the fused basic map
3865 * against the previously considered basic maps, starting at i + 1
3866 * (even if start2 is greater than i + 1).
3868 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3869 int start1, int end1, int start2, int end2)
3871 int i, j;
3873 for (i = end1 - 1; i >= start1; --i) {
3874 if (info[i].removed)
3875 continue;
3876 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3877 enum isl_change changed;
3879 if (info[j].removed)
3880 continue;
3881 if (info[i].removed)
3882 isl_die(ctx, isl_error_internal,
3883 "basic map unexpectedly removed",
3884 return -1);
3885 changed = coalesce_pair(i, j, info);
3886 switch (changed) {
3887 case isl_change_error:
3888 return -1;
3889 case isl_change_none:
3890 case isl_change_drop_second:
3891 continue;
3892 case isl_change_drop_first:
3893 j = end2;
3894 break;
3895 case isl_change_fuse:
3896 j = i;
3897 break;
3902 return 0;
3905 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3907 * We consider groups of basic maps that live in the same apparent
3908 * affine hull and we first coalesce within such a group before we
3909 * coalesce the elements in the group with elements of previously
3910 * considered groups. If a fuse happens during the second phase,
3911 * then we also reconsider the elements within the group.
3913 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3915 int start, end;
3917 for (end = n; end > 0; end = start) {
3918 start = end - 1;
3919 while (start >= 1 &&
3920 info[start - 1].hull_hash == info[start].hull_hash)
3921 start--;
3922 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3923 return -1;
3924 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3925 return -1;
3928 return 0;
3931 /* Update the basic maps in "map" based on the information in "info".
3932 * In particular, remove the basic maps that have been marked removed and
3933 * update the others based on the information in the corresponding tableau.
3934 * Since we detected implicit equalities without calling
3935 * isl_basic_map_gauss, we need to do it now.
3936 * Also call isl_basic_map_simplify if we may have lost the definition
3937 * of one or more integer divisions.
3938 * If a basic map is still equal to the one from which the corresponding "info"
3939 * entry was created, then redundant constraint and
3940 * implicit equality constraint detection have been performed
3941 * on the corresponding tableau and the basic map can be marked as such.
3943 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3944 int n, struct isl_coalesce_info *info)
3946 int i;
3948 if (!map)
3949 return NULL;
3951 for (i = n - 1; i >= 0; --i) {
3952 if (info[i].removed) {
3953 isl_basic_map_free(map->p[i]);
3954 if (i != map->n - 1)
3955 map->p[i] = map->p[map->n - 1];
3956 map->n--;
3957 continue;
3960 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3961 info[i].tab);
3962 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3963 if (info[i].simplify)
3964 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3965 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3966 if (!info[i].bmap)
3967 return isl_map_free(map);
3968 if (!info[i].modified) {
3969 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3970 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3972 isl_basic_map_free(map->p[i]);
3973 map->p[i] = info[i].bmap;
3974 info[i].bmap = NULL;
3977 return map;
3980 /* For each pair of basic maps in the map, check if the union of the two
3981 * can be represented by a single basic map.
3982 * If so, replace the pair by the single basic map and start over.
3984 * We factor out any (hidden) common factor from the constraint
3985 * coefficients to improve the detection of adjacent constraints.
3986 * Note that this function does not call isl_basic_map_gauss,
3987 * but it does make sure that only a single copy of the basic map
3988 * is affected. This means that isl_basic_map_gauss may have
3989 * to be called at the end of the computation (in update_basic_maps)
3990 * on this single copy to ensure that
3991 * the basic maps are not left in an unexpected state.
3993 * Since we are constructing the tableaus of the basic maps anyway,
3994 * we exploit them to detect implicit equalities and redundant constraints.
3995 * This also helps the coalescing as it can ignore the redundant constraints.
3996 * In order to avoid confusion, we make all implicit equalities explicit
3997 * in the basic maps. If the basic map only has a single reference
3998 * (this happens in particular if it was modified by
3999 * isl_basic_map_reduce_coefficients), then isl_basic_map_gauss
4000 * does not get called on the result. The call to
4001 * isl_basic_map_gauss in update_basic_maps resolves this as well.
4002 * For each basic map, we also compute the hash of the apparent affine hull
4003 * for use in coalesce.
4005 __isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
4007 int i;
4008 unsigned n;
4009 isl_ctx *ctx;
4010 struct isl_coalesce_info *info = NULL;
4012 map = isl_map_remove_empty_parts(map);
4013 if (!map)
4014 return NULL;
4016 if (map->n <= 1)
4017 return map;
4019 ctx = isl_map_get_ctx(map);
4020 map = isl_map_sort_divs(map);
4021 map = isl_map_cow(map);
4023 if (!map)
4024 return NULL;
4026 n = map->n;
4028 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
4029 if (!info)
4030 goto error;
4032 for (i = 0; i < map->n; ++i) {
4033 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
4034 if (!map->p[i])
4035 goto error;
4036 info[i].bmap = isl_basic_map_copy(map->p[i]);
4037 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
4038 if (!info[i].tab)
4039 goto error;
4040 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
4041 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
4042 goto error;
4043 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
4044 info[i].bmap);
4045 if (!info[i].bmap)
4046 goto error;
4047 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
4048 if (isl_tab_detect_redundant(info[i].tab) < 0)
4049 goto error;
4050 if (coalesce_info_set_hull_hash(&info[i]) < 0)
4051 goto error;
4053 for (i = map->n - 1; i >= 0; --i)
4054 if (info[i].tab->empty)
4055 drop(&info[i]);
4057 if (coalesce(ctx, n, info) < 0)
4058 goto error;
4060 map = update_basic_maps(map, n, info);
4062 clear_coalesce_info(n, info);
4064 return map;
4065 error:
4066 clear_coalesce_info(n, info);
4067 isl_map_free(map);
4068 return NULL;
4071 /* For each pair of basic sets in the set, check if the union of the two
4072 * can be represented by a single basic set.
4073 * If so, replace the pair by the single basic set and start over.
4075 struct isl_set *isl_set_coalesce(struct isl_set *set)
4077 return set_from_map(isl_map_coalesce(set_to_map(set)));