add isl_local_div_is_marked_unknown
[isl.git] / isl_coalesce.c
blob09d9c7cc20355c13eed604e85b9e62af28070c36
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_vec_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_equalities.h>
32 #include <set_to_map.c>
33 #include <set_from_map.c>
35 #define STATUS_ERROR -1
36 #define STATUS_REDUNDANT 1
37 #define STATUS_VALID 2
38 #define STATUS_SEPARATE 3
39 #define STATUS_CUT 4
40 #define STATUS_ADJ_EQ 5
41 #define STATUS_ADJ_INEQ 6
43 static int status_in(isl_int *ineq, struct isl_tab *tab)
45 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
46 switch (type) {
47 default:
48 case isl_ineq_error: return STATUS_ERROR;
49 case isl_ineq_redundant: return STATUS_VALID;
50 case isl_ineq_separate: return STATUS_SEPARATE;
51 case isl_ineq_cut: return STATUS_CUT;
52 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
53 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
57 /* Compute the position of the equalities of basic map "bmap_i"
58 * with respect to the basic map represented by "tab_j".
59 * The resulting array has twice as many entries as the number
60 * of equalities corresponding to the two inequalties to which
61 * each equality corresponds.
63 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
64 struct isl_tab *tab_j)
66 int k, l;
67 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
68 unsigned dim;
70 if (!eq)
71 return NULL;
73 dim = isl_basic_map_total_dim(bmap_i);
74 for (k = 0; k < bmap_i->n_eq; ++k) {
75 for (l = 0; l < 2; ++l) {
76 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
77 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
78 if (eq[2 * k + l] == STATUS_ERROR)
79 goto error;
81 if (eq[2 * k] == STATUS_SEPARATE ||
82 eq[2 * k + 1] == STATUS_SEPARATE)
83 break;
86 return eq;
87 error:
88 free(eq);
89 return NULL;
92 /* Compute the position of the inequalities of basic map "bmap_i"
93 * (also represented by "tab_i", if not NULL) with respect to the basic map
94 * represented by "tab_j".
96 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
97 struct isl_tab *tab_i, struct isl_tab *tab_j)
99 int k;
100 unsigned n_eq = bmap_i->n_eq;
101 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
103 if (!ineq)
104 return NULL;
106 for (k = 0; k < bmap_i->n_ineq; ++k) {
107 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
108 ineq[k] = STATUS_REDUNDANT;
109 continue;
111 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
112 if (ineq[k] == STATUS_ERROR)
113 goto error;
114 if (ineq[k] == STATUS_SEPARATE)
115 break;
118 return ineq;
119 error:
120 free(ineq);
121 return NULL;
124 static int any(int *con, unsigned len, int status)
126 int i;
128 for (i = 0; i < len ; ++i)
129 if (con[i] == status)
130 return 1;
131 return 0;
134 /* Return the first position of "status" in the list "con" of length "len".
135 * Return -1 if there is no such entry.
137 static int find(int *con, unsigned len, int status)
139 int i;
141 for (i = 0; i < len ; ++i)
142 if (con[i] == status)
143 return i;
144 return -1;
147 static int count(int *con, unsigned len, int status)
149 int i;
150 int c = 0;
152 for (i = 0; i < len ; ++i)
153 if (con[i] == status)
154 c++;
155 return c;
158 static int all(int *con, unsigned len, int status)
160 int i;
162 for (i = 0; i < len ; ++i) {
163 if (con[i] == STATUS_REDUNDANT)
164 continue;
165 if (con[i] != status)
166 return 0;
168 return 1;
171 /* Internal information associated to a basic map in a map
172 * that is to be coalesced by isl_map_coalesce.
174 * "bmap" is the basic map itself (or NULL if "removed" is set)
175 * "tab" is the corresponding tableau (or NULL if "removed" is set)
176 * "hull_hash" identifies the affine space in which "bmap" lives.
177 * "removed" is set if this basic map has been removed from the map
178 * "simplify" is set if this basic map may have some unknown integer
179 * divisions that were not present in the input basic maps. The basic
180 * map should then be simplified such that we may be able to find
181 * a definition among the constraints.
183 * "eq" and "ineq" are only set if we are currently trying to coalesce
184 * this basic map with another basic map, in which case they represent
185 * the position of the inequalities of this basic map with respect to
186 * the other basic map. The number of elements in the "eq" array
187 * is twice the number of equalities in the "bmap", corresponding
188 * to the two inequalities that make up each equality.
190 struct isl_coalesce_info {
191 isl_basic_map *bmap;
192 struct isl_tab *tab;
193 uint32_t hull_hash;
194 int removed;
195 int simplify;
196 int *eq;
197 int *ineq;
200 /* Are all non-redundant constraints of the basic map represented by "info"
201 * either valid or cut constraints with respect to the other basic map?
203 static int all_valid_or_cut(struct isl_coalesce_info *info)
205 int i;
207 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
208 if (info->eq[i] == STATUS_REDUNDANT)
209 continue;
210 if (info->eq[i] == STATUS_VALID)
211 continue;
212 if (info->eq[i] == STATUS_CUT)
213 continue;
214 return 0;
217 for (i = 0; i < info->bmap->n_ineq; ++i) {
218 if (info->ineq[i] == STATUS_REDUNDANT)
219 continue;
220 if (info->ineq[i] == STATUS_VALID)
221 continue;
222 if (info->ineq[i] == STATUS_CUT)
223 continue;
224 return 0;
227 return 1;
230 /* Compute the hash of the (apparent) affine hull of info->bmap (with
231 * the existentially quantified variables removed) and store it
232 * in info->hash.
234 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
236 isl_basic_map *hull;
237 unsigned n_div;
239 hull = isl_basic_map_copy(info->bmap);
240 hull = isl_basic_map_plain_affine_hull(hull);
241 n_div = isl_basic_map_dim(hull, isl_dim_div);
242 hull = isl_basic_map_drop_constraints_involving_dims(hull,
243 isl_dim_div, 0, n_div);
244 info->hull_hash = isl_basic_map_get_hash(hull);
245 isl_basic_map_free(hull);
247 return hull ? 0 : -1;
250 /* Free all the allocated memory in an array
251 * of "n" isl_coalesce_info elements.
253 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
255 int i;
257 if (!info)
258 return;
260 for (i = 0; i < n; ++i) {
261 isl_basic_map_free(info[i].bmap);
262 isl_tab_free(info[i].tab);
265 free(info);
268 /* Drop the basic map represented by "info".
269 * That is, clear the memory associated to the entry and
270 * mark it as having been removed.
272 static void drop(struct isl_coalesce_info *info)
274 info->bmap = isl_basic_map_free(info->bmap);
275 isl_tab_free(info->tab);
276 info->tab = NULL;
277 info->removed = 1;
280 /* Exchange the information in "info1" with that in "info2".
282 static void exchange(struct isl_coalesce_info *info1,
283 struct isl_coalesce_info *info2)
285 struct isl_coalesce_info info;
287 info = *info1;
288 *info1 = *info2;
289 *info2 = info;
292 /* This type represents the kind of change that has been performed
293 * while trying to coalesce two basic maps.
295 * isl_change_none: nothing was changed
296 * isl_change_drop_first: the first basic map was removed
297 * isl_change_drop_second: the second basic map was removed
298 * isl_change_fuse: the two basic maps were replaced by a new basic map.
300 enum isl_change {
301 isl_change_error = -1,
302 isl_change_none = 0,
303 isl_change_drop_first,
304 isl_change_drop_second,
305 isl_change_fuse,
308 /* Update "change" based on an interchange of the first and the second
309 * basic map. That is, interchange isl_change_drop_first and
310 * isl_change_drop_second.
312 static enum isl_change invert_change(enum isl_change change)
314 switch (change) {
315 case isl_change_error:
316 return isl_change_error;
317 case isl_change_none:
318 return isl_change_none;
319 case isl_change_drop_first:
320 return isl_change_drop_second;
321 case isl_change_drop_second:
322 return isl_change_drop_first;
323 case isl_change_fuse:
324 return isl_change_fuse;
327 return isl_change_error;
330 /* Add the valid constraints of the basic map represented by "info"
331 * to "bmap". "len" is the size of the constraints.
332 * If only one of the pair of inequalities that make up an equality
333 * is valid, then add that inequality.
335 static __isl_give isl_basic_map *add_valid_constraints(
336 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
337 unsigned len)
339 int k, l;
341 if (!bmap)
342 return NULL;
344 for (k = 0; k < info->bmap->n_eq; ++k) {
345 if (info->eq[2 * k] == STATUS_VALID &&
346 info->eq[2 * k + 1] == STATUS_VALID) {
347 l = isl_basic_map_alloc_equality(bmap);
348 if (l < 0)
349 return isl_basic_map_free(bmap);
350 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
351 } else if (info->eq[2 * k] == STATUS_VALID) {
352 l = isl_basic_map_alloc_inequality(bmap);
353 if (l < 0)
354 return isl_basic_map_free(bmap);
355 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
356 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
357 l = isl_basic_map_alloc_inequality(bmap);
358 if (l < 0)
359 return isl_basic_map_free(bmap);
360 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
364 for (k = 0; k < info->bmap->n_ineq; ++k) {
365 if (info->ineq[k] != STATUS_VALID)
366 continue;
367 l = isl_basic_map_alloc_inequality(bmap);
368 if (l < 0)
369 return isl_basic_map_free(bmap);
370 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
373 return bmap;
376 /* Is "bmap" defined by a number of (non-redundant) constraints that
377 * is greater than the number of constraints of basic maps i and j combined?
378 * Equalities are counted as two inequalities.
380 static int number_of_constraints_increases(int i, int j,
381 struct isl_coalesce_info *info,
382 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
384 int k, n_old, n_new;
386 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
387 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
389 n_new = 2 * bmap->n_eq;
390 for (k = 0; k < bmap->n_ineq; ++k)
391 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
392 ++n_new;
394 return n_new > n_old;
397 /* Replace the pair of basic maps i and j by the basic map bounded
398 * by the valid constraints in both basic maps and the constraints
399 * in extra (if not NULL).
400 * Place the fused basic map in the position that is the smallest of i and j.
402 * If "detect_equalities" is set, then look for equalities encoded
403 * as pairs of inequalities.
404 * If "check_number" is set, then the original basic maps are only
405 * replaced if the total number of constraints does not increase.
406 * While the number of integer divisions in the two basic maps
407 * is assumed to be the same, the actual definitions may be different.
408 * We only copy the definition from one of the basic map if it is
409 * the same as that of the other basic map. Otherwise, we mark
410 * the integer division as unknown and simplify the basic map
411 * in an attempt to recover the integer division definition.
413 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
414 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
416 int k, l;
417 struct isl_basic_map *fused = NULL;
418 struct isl_tab *fused_tab = NULL;
419 unsigned total = isl_basic_map_total_dim(info[i].bmap);
420 unsigned extra_rows = extra ? extra->n_row : 0;
421 unsigned n_eq, n_ineq;
422 int simplify = 0;
424 if (j < i)
425 return fuse(j, i, info, extra, detect_equalities, check_number);
427 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
428 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
429 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
430 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
431 fused = add_valid_constraints(fused, &info[i], 1 + total);
432 fused = add_valid_constraints(fused, &info[j], 1 + total);
433 if (!fused)
434 goto error;
435 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
436 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
437 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
439 for (k = 0; k < info[i].bmap->n_div; ++k) {
440 int l = isl_basic_map_alloc_div(fused);
441 if (l < 0)
442 goto error;
443 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
444 1 + 1 + total)) {
445 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
446 1 + 1 + total);
447 } else {
448 isl_int_set_si(fused->div[l][0], 0);
449 simplify = 1;
453 for (k = 0; k < extra_rows; ++k) {
454 l = isl_basic_map_alloc_inequality(fused);
455 if (l < 0)
456 goto error;
457 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
460 if (detect_equalities)
461 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
462 fused = isl_basic_map_gauss(fused, NULL);
463 if (simplify || info[j].simplify) {
464 fused = isl_basic_map_simplify(fused);
465 info[i].simplify = 0;
467 fused = isl_basic_map_finalize(fused);
469 fused_tab = isl_tab_from_basic_map(fused, 0);
470 if (isl_tab_detect_redundant(fused_tab) < 0)
471 goto error;
473 if (check_number &&
474 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
475 isl_tab_free(fused_tab);
476 isl_basic_map_free(fused);
477 return isl_change_none;
480 isl_basic_map_free(info[i].bmap);
481 info[i].bmap = fused;
482 isl_tab_free(info[i].tab);
483 info[i].tab = fused_tab;
484 drop(&info[j]);
486 return isl_change_fuse;
487 error:
488 isl_tab_free(fused_tab);
489 isl_basic_map_free(fused);
490 return isl_change_error;
493 /* Given a pair of basic maps i and j such that all constraints are either
494 * "valid" or "cut", check if the facets corresponding to the "cut"
495 * constraints of i lie entirely within basic map j.
496 * If so, replace the pair by the basic map consisting of the valid
497 * constraints in both basic maps.
498 * Checking whether the facet lies entirely within basic map j
499 * is performed by checking whether the constraints of basic map j
500 * are valid for the facet. These tests are performed on a rational
501 * tableau to avoid the theoretical possibility that a constraint
502 * that was considered to be a cut constraint for the entire basic map i
503 * happens to be considered to be a valid constraint for the facet,
504 * even though it cuts off the same rational points.
506 * To see that we are not introducing any extra points, call the
507 * two basic maps A and B and the resulting map U and let x
508 * be an element of U \setminus ( A \cup B ).
509 * A line connecting x with an element of A \cup B meets a facet F
510 * of either A or B. Assume it is a facet of B and let c_1 be
511 * the corresponding facet constraint. We have c_1(x) < 0 and
512 * so c_1 is a cut constraint. This implies that there is some
513 * (possibly rational) point x' satisfying the constraints of A
514 * and the opposite of c_1 as otherwise c_1 would have been marked
515 * valid for A. The line connecting x and x' meets a facet of A
516 * in a (possibly rational) point that also violates c_1, but this
517 * is impossible since all cut constraints of B are valid for all
518 * cut facets of A.
519 * In case F is a facet of A rather than B, then we can apply the
520 * above reasoning to find a facet of B separating x from A \cup B first.
522 static enum isl_change check_facets(int i, int j,
523 struct isl_coalesce_info *info)
525 int k, l;
526 struct isl_tab_undo *snap, *snap2;
527 unsigned n_eq = info[i].bmap->n_eq;
529 snap = isl_tab_snap(info[i].tab);
530 if (isl_tab_mark_rational(info[i].tab) < 0)
531 return isl_change_error;
532 snap2 = isl_tab_snap(info[i].tab);
534 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
535 if (info[i].ineq[k] != STATUS_CUT)
536 continue;
537 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
538 return isl_change_error;
539 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
540 int stat;
541 if (info[j].ineq[l] != STATUS_CUT)
542 continue;
543 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
544 if (stat < 0)
545 return isl_change_error;
546 if (stat != STATUS_VALID)
547 break;
549 if (isl_tab_rollback(info[i].tab, snap2) < 0)
550 return isl_change_error;
551 if (l < info[j].bmap->n_ineq)
552 break;
555 if (k < info[i].bmap->n_ineq) {
556 if (isl_tab_rollback(info[i].tab, snap) < 0)
557 return isl_change_error;
558 return isl_change_none;
560 return fuse(i, j, info, NULL, 0, 0);
563 /* Check if info->bmap contains the basic map represented
564 * by the tableau "tab".
565 * For each equality, we check both the constraint itself
566 * (as an inequality) and its negation. Make sure the
567 * equality is returned to its original state before returning.
569 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
571 int k;
572 unsigned dim;
573 isl_basic_map *bmap = info->bmap;
575 dim = isl_basic_map_total_dim(bmap);
576 for (k = 0; k < bmap->n_eq; ++k) {
577 int stat;
578 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
579 stat = status_in(bmap->eq[k], tab);
580 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
581 if (stat < 0)
582 return -1;
583 if (stat != STATUS_VALID)
584 return 0;
585 stat = status_in(bmap->eq[k], tab);
586 if (stat < 0)
587 return -1;
588 if (stat != STATUS_VALID)
589 return 0;
592 for (k = 0; k < bmap->n_ineq; ++k) {
593 int stat;
594 if (info->ineq[k] == STATUS_REDUNDANT)
595 continue;
596 stat = status_in(bmap->ineq[k], tab);
597 if (stat < 0)
598 return -1;
599 if (stat != STATUS_VALID)
600 return 0;
602 return 1;
605 /* Basic map "i" has an inequality (say "k") that is adjacent
606 * to some inequality of basic map "j". All the other inequalities
607 * are valid for "j".
608 * Check if basic map "j" forms an extension of basic map "i".
610 * Note that this function is only called if some of the equalities or
611 * inequalities of basic map "j" do cut basic map "i". The function is
612 * correct even if there are no such cut constraints, but in that case
613 * the additional checks performed by this function are overkill.
615 * In particular, we replace constraint k, say f >= 0, by constraint
616 * f <= -1, add the inequalities of "j" that are valid for "i"
617 * and check if the result is a subset of basic map "j".
618 * To improve the chances of the subset relation being detected,
619 * any variable that only attains a single integer value
620 * in the tableau of "i" is first fixed to that value.
621 * If the result is a subset, then we know that this result is exactly equal
622 * to basic map "j" since all its constraints are valid for basic map "j".
623 * By combining the valid constraints of "i" (all equalities and all
624 * inequalities except "k") and the valid constraints of "j" we therefore
625 * obtain a basic map that is equal to their union.
626 * In this case, there is no need to perform a rollback of the tableau
627 * since it is going to be destroyed in fuse().
630 * |\__ |\__
631 * | \__ | \__
632 * | \_ => | \__
633 * |_______| _ |_________\
636 * |\ |\
637 * | \ | \
638 * | \ | \
639 * | | | \
640 * | ||\ => | \
641 * | || \ | \
642 * | || | | |
643 * |__||_/ |_____/
645 static enum isl_change is_adj_ineq_extension(int i, int j,
646 struct isl_coalesce_info *info)
648 int k;
649 struct isl_tab_undo *snap;
650 unsigned n_eq = info[i].bmap->n_eq;
651 unsigned total = isl_basic_map_total_dim(info[i].bmap);
652 int r;
653 int super;
655 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
656 return isl_change_error;
658 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
659 if (k < 0)
660 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
661 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
662 return isl_change_error);
664 snap = isl_tab_snap(info[i].tab);
666 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
667 return isl_change_error;
669 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
670 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
671 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
672 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
673 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
674 if (r < 0)
675 return isl_change_error;
677 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
678 if (info[j].ineq[k] != STATUS_VALID)
679 continue;
680 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
681 return isl_change_error;
683 if (isl_tab_detect_constants(info[i].tab) < 0)
684 return isl_change_error;
686 super = contains(&info[j], info[i].tab);
687 if (super < 0)
688 return isl_change_error;
689 if (super)
690 return fuse(i, j, info, NULL, 0, 0);
692 if (isl_tab_rollback(info[i].tab, snap) < 0)
693 return isl_change_error;
695 return isl_change_none;
699 /* Both basic maps have at least one inequality with and adjacent
700 * (but opposite) inequality in the other basic map.
701 * Check that there are no cut constraints and that there is only
702 * a single pair of adjacent inequalities.
703 * If so, we can replace the pair by a single basic map described
704 * by all but the pair of adjacent inequalities.
705 * Any additional points introduced lie strictly between the two
706 * adjacent hyperplanes and can therefore be integral.
708 * ____ _____
709 * / ||\ / \
710 * / || \ / \
711 * \ || \ => \ \
712 * \ || / \ /
713 * \___||_/ \_____/
715 * The test for a single pair of adjancent inequalities is important
716 * for avoiding the combination of two basic maps like the following
718 * /|
719 * / |
720 * /__|
721 * _____
722 * | |
723 * | |
724 * |___|
726 * If there are some cut constraints on one side, then we may
727 * still be able to fuse the two basic maps, but we need to perform
728 * some additional checks in is_adj_ineq_extension.
730 static enum isl_change check_adj_ineq(int i, int j,
731 struct isl_coalesce_info *info)
733 int count_i, count_j;
734 int cut_i, cut_j;
736 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
737 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
739 if (count_i != 1 && count_j != 1)
740 return isl_change_none;
742 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
743 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
744 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
745 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
747 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
748 return fuse(i, j, info, NULL, 0, 0);
750 if (count_i == 1 && !cut_i)
751 return is_adj_ineq_extension(i, j, info);
753 if (count_j == 1 && !cut_j)
754 return is_adj_ineq_extension(j, i, info);
756 return isl_change_none;
759 /* Given an affine transformation matrix "T", does row "row" represent
760 * anything other than a unit vector (possibly shifted by a constant)
761 * that is not involved in any of the other rows?
763 * That is, if a constraint involves the variable corresponding to
764 * the row, then could its preimage by "T" have any coefficients
765 * that are different from those in the original constraint?
767 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
769 int i, j;
770 int len = T->n_col - 1;
772 i = isl_seq_first_non_zero(T->row[row] + 1, len);
773 if (i < 0)
774 return 1;
775 if (!isl_int_is_one(T->row[row][1 + i]) &&
776 !isl_int_is_negone(T->row[row][1 + i]))
777 return 1;
779 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
780 if (j >= 0)
781 return 1;
783 for (j = 1; j < T->n_row; ++j) {
784 if (j == row)
785 continue;
786 if (!isl_int_is_zero(T->row[j][1 + i]))
787 return 1;
790 return 0;
793 /* Does inequality constraint "ineq" of "bmap" involve any of
794 * the variables marked in "affected"?
795 * "total" is the total number of variables, i.e., the number
796 * of entries in "affected".
798 static int is_affected(__isl_keep isl_basic_map *bmap, int ineq, int *affected,
799 int total)
801 int i;
803 for (i = 0; i < total; ++i) {
804 if (!affected[i])
805 continue;
806 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
807 return 1;
810 return 0;
813 /* Given the compressed version of inequality constraint "ineq"
814 * of info->bmap in "v", check if the constraint can be tightened,
815 * where the compression is based on an equality constraint valid
816 * for info->tab.
817 * If so, add the tightened version of the inequality constraint
818 * to info->tab. "v" may be modified by this function.
820 * That is, if the compressed constraint is of the form
822 * m f() + c >= 0
824 * with 0 < c < m, then it is equivalent to
826 * f() >= 0
828 * This means that c can also be subtracted from the original,
829 * uncompressed constraint without affecting the integer points
830 * in info->tab. Add this tightened constraint as an extra row
831 * to info->tab to make this information explicitly available.
833 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
834 int ineq, __isl_take isl_vec *v)
836 isl_ctx *ctx;
837 int r;
839 if (!v)
840 return NULL;
842 ctx = isl_vec_get_ctx(v);
843 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
844 if (isl_int_is_zero(ctx->normalize_gcd) ||
845 isl_int_is_one(ctx->normalize_gcd)) {
846 return v;
849 v = isl_vec_cow(v);
850 if (!v)
851 return NULL;
853 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
854 if (isl_int_is_zero(v->el[0]))
855 return v;
857 if (isl_tab_extend_cons(info->tab, 1) < 0)
858 return isl_vec_free(v);
860 isl_int_sub(info->bmap->ineq[ineq][0],
861 info->bmap->ineq[ineq][0], v->el[0]);
862 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
863 isl_int_add(info->bmap->ineq[ineq][0],
864 info->bmap->ineq[ineq][0], v->el[0]);
866 if (r < 0)
867 return isl_vec_free(v);
869 return v;
872 /* Tighten the (non-redundant) constraints on the facet represented
873 * by info->tab.
874 * In particular, on input, info->tab represents the result
875 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
876 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
877 * replacing the one at index "l" by the corresponding equality,
878 * i.e., f_k + 1 = 0, with k = relaxed[l].
880 * Compute a variable compression from the equality constraint f_k + 1 = 0
881 * and use it to tighten the other constraints of info->bmap
882 * (that is, all constraints that have not been relaxed),
883 * updating info->tab (and leaving info->bmap untouched).
884 * The compression handles essentially two cases, one where a variable
885 * is assigned a fixed value and can therefore be eliminated, and one
886 * where one variable is a shifted multiple of some other variable and
887 * can therefore be replaced by that multiple.
888 * Gaussian elimination would also work for the first case, but for
889 * the second case, the effectiveness would depend on the order
890 * of the variables.
891 * After compression, some of the constraints may have coefficients
892 * with a common divisor. If this divisor does not divide the constant
893 * term, then the constraint can be tightened.
894 * The tightening is performed on the tableau info->tab by introducing
895 * extra (temporary) constraints.
897 * Only constraints that are possibly affected by the compression are
898 * considered. In particular, if the constraint only involves variables
899 * that are directly mapped to a distinct set of other variables, then
900 * no common divisor can be introduced and no tightening can occur.
902 * It is important to only consider the non-redundant constraints
903 * since the facet constraint has been relaxed prior to the call
904 * to this function, meaning that the constraints that were redundant
905 * prior to the relaxation may no longer be redundant.
906 * These constraints will be ignored in the fused result, so
907 * the fusion detection should not exploit them.
909 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
910 int n, int *relaxed, int l)
912 unsigned total;
913 isl_ctx *ctx;
914 isl_vec *v = NULL;
915 isl_mat *T;
916 int i;
917 int k;
918 int *affected;
920 k = relaxed[l];
921 ctx = isl_basic_map_get_ctx(info->bmap);
922 total = isl_basic_map_total_dim(info->bmap);
923 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
924 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
925 T = isl_mat_variable_compression(T, NULL);
926 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
927 if (!T)
928 return isl_stat_error;
929 if (T->n_col == 0) {
930 isl_mat_free(T);
931 return isl_stat_ok;
934 affected = isl_alloc_array(ctx, int, total);
935 if (!affected)
936 goto error;
938 for (i = 0; i < total; ++i)
939 affected[i] = not_unique_unit_row(T, 1 + i);
941 for (i = 0; i < info->bmap->n_ineq; ++i) {
942 if (any(relaxed, n, i))
943 continue;
944 if (info->ineq[i] == STATUS_REDUNDANT)
945 continue;
946 if (!is_affected(info->bmap, i, affected, total))
947 continue;
948 v = isl_vec_alloc(ctx, 1 + total);
949 if (!v)
950 goto error;
951 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
952 v = isl_vec_mat_product(v, isl_mat_copy(T));
953 v = try_tightening(info, i, v);
954 isl_vec_free(v);
955 if (!v)
956 goto error;
959 isl_mat_free(T);
960 free(affected);
961 return isl_stat_ok;
962 error:
963 isl_mat_free(T);
964 free(affected);
965 return isl_stat_error;
968 /* Replace the basic maps "i" and "j" by an extension of "i"
969 * along the "n" inequality constraints in "relax" by one.
970 * The tableau info[i].tab has already been extended.
971 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
972 * by one.
973 * Each integer division that does not have exactly the same
974 * definition in "i" and "j" is marked unknown and the basic map
975 * is scheduled to be simplified in an attempt to recover
976 * the integer division definition.
977 * Place the extension in the position that is the smallest of i and j.
979 static enum isl_change extend(int i, int j, int n, int *relax,
980 struct isl_coalesce_info *info)
982 int l;
983 unsigned total;
985 info[i].bmap = isl_basic_map_cow(info[i].bmap);
986 if (!info[i].bmap)
987 return isl_change_error;
988 total = isl_basic_map_total_dim(info[i].bmap);
989 for (l = 0; l < info[i].bmap->n_div; ++l)
990 if (!isl_seq_eq(info[i].bmap->div[l],
991 info[j].bmap->div[l], 1 + 1 + total)) {
992 isl_int_set_si(info[i].bmap->div[l][0], 0);
993 info[i].simplify = 1;
995 for (l = 0; l < n; ++l)
996 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
997 info[i].bmap->ineq[relax[l]][0], 1);
998 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
999 drop(&info[j]);
1000 if (j < i)
1001 exchange(&info[i], &info[j]);
1002 return isl_change_fuse;
1005 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1006 * that are such that they include basic map "j" if they are relaxed
1007 * by one. All the other inequalities are valid for "j".
1008 * Check if basic map "j" forms an extension of basic map "i".
1010 * In particular, relax the constraints in "relax", compute the corresponding
1011 * facets one by one and check whether each of these is included
1012 * in the other basic map.
1013 * Before testing for inclusion, the constraints on each facet
1014 * are tightened to increase the chance of an inclusion being detected.
1015 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1016 * in is_adj_ineq_extension, may further increase those chances, but this
1017 * is not currently done.)
1018 * If each facet is included, we know that relaxing the constraints extends
1019 * the basic map with exactly the other basic map (we already know that this
1020 * other basic map is included in the extension, because all other
1021 * inequality constraints are valid of "j") and we can replace the
1022 * two basic maps by this extension.
1023 * ____ _____
1024 * / || / |
1025 * / || / |
1026 * \ || => \ |
1027 * \ || \ |
1028 * \___|| \____|
1031 * \ |\
1032 * |\\ | \
1033 * | \\ | \
1034 * | | => | /
1035 * | / | /
1036 * |/ |/
1038 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1039 struct isl_coalesce_info *info)
1041 int l;
1042 int super;
1043 struct isl_tab_undo *snap, *snap2;
1044 unsigned n_eq = info[i].bmap->n_eq;
1046 for (l = 0; l < n; ++l)
1047 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1048 return isl_change_none;
1050 snap = isl_tab_snap(info[i].tab);
1051 for (l = 0; l < n; ++l)
1052 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1053 return isl_change_error;
1054 snap2 = isl_tab_snap(info[i].tab);
1055 for (l = 0; l < n; ++l) {
1056 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1057 return isl_change_error;
1058 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1059 return isl_change_error;
1060 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1061 return isl_change_error;
1062 super = contains(&info[j], info[i].tab);
1063 if (super < 0)
1064 return isl_change_error;
1065 if (super)
1066 continue;
1067 if (isl_tab_rollback(info[i].tab, snap) < 0)
1068 return isl_change_error;
1069 return isl_change_none;
1072 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1073 return isl_change_error;
1074 return extend(i, j, n, relax, info);
1077 /* Data structure that keeps track of the wrapping constraints
1078 * and of information to bound the coefficients of those constraints.
1080 * bound is set if we want to apply a bound on the coefficients
1081 * mat contains the wrapping constraints
1082 * max is the bound on the coefficients (if bound is set)
1084 struct isl_wraps {
1085 int bound;
1086 isl_mat *mat;
1087 isl_int max;
1090 /* Update wraps->max to be greater than or equal to the coefficients
1091 * in the equalities and inequalities of info->bmap that can be removed
1092 * if we end up applying wrapping.
1094 static void wraps_update_max(struct isl_wraps *wraps,
1095 struct isl_coalesce_info *info)
1097 int k;
1098 isl_int max_k;
1099 unsigned total = isl_basic_map_total_dim(info->bmap);
1101 isl_int_init(max_k);
1103 for (k = 0; k < info->bmap->n_eq; ++k) {
1104 if (info->eq[2 * k] == STATUS_VALID &&
1105 info->eq[2 * k + 1] == STATUS_VALID)
1106 continue;
1107 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1108 if (isl_int_abs_gt(max_k, wraps->max))
1109 isl_int_set(wraps->max, max_k);
1112 for (k = 0; k < info->bmap->n_ineq; ++k) {
1113 if (info->ineq[k] == STATUS_VALID ||
1114 info->ineq[k] == STATUS_REDUNDANT)
1115 continue;
1116 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1117 if (isl_int_abs_gt(max_k, wraps->max))
1118 isl_int_set(wraps->max, max_k);
1121 isl_int_clear(max_k);
1124 /* Initialize the isl_wraps data structure.
1125 * If we want to bound the coefficients of the wrapping constraints,
1126 * we set wraps->max to the largest coefficient
1127 * in the equalities and inequalities that can be removed if we end up
1128 * applying wrapping.
1130 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1131 struct isl_coalesce_info *info, int i, int j)
1133 isl_ctx *ctx;
1135 wraps->bound = 0;
1136 wraps->mat = mat;
1137 if (!mat)
1138 return;
1139 ctx = isl_mat_get_ctx(mat);
1140 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1141 if (!wraps->bound)
1142 return;
1143 isl_int_init(wraps->max);
1144 isl_int_set_si(wraps->max, 0);
1145 wraps_update_max(wraps, &info[i]);
1146 wraps_update_max(wraps, &info[j]);
1149 /* Free the contents of the isl_wraps data structure.
1151 static void wraps_free(struct isl_wraps *wraps)
1153 isl_mat_free(wraps->mat);
1154 if (wraps->bound)
1155 isl_int_clear(wraps->max);
1158 /* Is the wrapping constraint in row "row" allowed?
1160 * If wraps->bound is set, we check that none of the coefficients
1161 * is greater than wraps->max.
1163 static int allow_wrap(struct isl_wraps *wraps, int row)
1165 int i;
1167 if (!wraps->bound)
1168 return 1;
1170 for (i = 1; i < wraps->mat->n_col; ++i)
1171 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1172 return 0;
1174 return 1;
1177 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1178 * to include "set" and add the result in position "w" of "wraps".
1179 * "len" is the total number of coefficients in "bound" and "ineq".
1180 * Return 1 on success, 0 on failure and -1 on error.
1181 * Wrapping can fail if the result of wrapping is equal to "bound"
1182 * or if we want to bound the sizes of the coefficients and
1183 * the wrapped constraint does not satisfy this bound.
1185 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1186 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1188 isl_seq_cpy(wraps->mat->row[w], bound, len);
1189 if (negate) {
1190 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1191 ineq = wraps->mat->row[w + 1];
1193 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1194 return -1;
1195 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1196 return 0;
1197 if (!allow_wrap(wraps, w))
1198 return 0;
1199 return 1;
1202 /* For each constraint in info->bmap that is not redundant (as determined
1203 * by info->tab) and that is not a valid constraint for the other basic map,
1204 * wrap the constraint around "bound" such that it includes the whole
1205 * set "set" and append the resulting constraint to "wraps".
1206 * Note that the constraints that are valid for the other basic map
1207 * will be added to the combined basic map by default, so there is
1208 * no need to wrap them.
1209 * The caller wrap_in_facets even relies on this function not wrapping
1210 * any constraints that are already valid.
1211 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1212 * wraps->n_row is the number of actual wrapped constraints that have
1213 * been added.
1214 * If any of the wrapping problems results in a constraint that is
1215 * identical to "bound", then this means that "set" is unbounded in such
1216 * way that no wrapping is possible. If this happens then wraps->n_row
1217 * is reset to zero.
1218 * Similarly, if we want to bound the coefficients of the wrapping
1219 * constraints and a newly added wrapping constraint does not
1220 * satisfy the bound, then wraps->n_row is also reset to zero.
1222 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1223 isl_int *bound, __isl_keep isl_set *set)
1225 int l, m;
1226 int w;
1227 int added;
1228 isl_basic_map *bmap = info->bmap;
1229 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1231 w = wraps->mat->n_row;
1233 for (l = 0; l < bmap->n_ineq; ++l) {
1234 if (info->ineq[l] == STATUS_VALID ||
1235 info->ineq[l] == STATUS_REDUNDANT)
1236 continue;
1237 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1238 continue;
1239 if (isl_seq_eq(bound, bmap->ineq[l], len))
1240 continue;
1241 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1242 continue;
1244 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1245 if (added < 0)
1246 return -1;
1247 if (!added)
1248 goto unbounded;
1249 ++w;
1251 for (l = 0; l < bmap->n_eq; ++l) {
1252 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1253 continue;
1254 if (isl_seq_eq(bound, bmap->eq[l], len))
1255 continue;
1257 for (m = 0; m < 2; ++m) {
1258 if (info->eq[2 * l + m] == STATUS_VALID)
1259 continue;
1260 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1261 set, !m);
1262 if (added < 0)
1263 return -1;
1264 if (!added)
1265 goto unbounded;
1266 ++w;
1270 wraps->mat->n_row = w;
1271 return 0;
1272 unbounded:
1273 wraps->mat->n_row = 0;
1274 return 0;
1277 /* Check if the constraints in "wraps" from "first" until the last
1278 * are all valid for the basic set represented by "tab".
1279 * If not, wraps->n_row is set to zero.
1281 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1282 struct isl_tab *tab)
1284 int i;
1286 for (i = first; i < wraps->n_row; ++i) {
1287 enum isl_ineq_type type;
1288 type = isl_tab_ineq_type(tab, wraps->row[i]);
1289 if (type == isl_ineq_error)
1290 return -1;
1291 if (type == isl_ineq_redundant)
1292 continue;
1293 wraps->n_row = 0;
1294 return 0;
1297 return 0;
1300 /* Return a set that corresponds to the non-redundant constraints
1301 * (as recorded in tab) of bmap.
1303 * It's important to remove the redundant constraints as some
1304 * of the other constraints may have been modified after the
1305 * constraints were marked redundant.
1306 * In particular, a constraint may have been relaxed.
1307 * Redundant constraints are ignored when a constraint is relaxed
1308 * and should therefore continue to be ignored ever after.
1309 * Otherwise, the relaxation might be thwarted by some of
1310 * these constraints.
1312 * Update the underlying set to ensure that the dimension doesn't change.
1313 * Otherwise the integer divisions could get dropped if the tab
1314 * turns out to be empty.
1316 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1317 struct isl_tab *tab)
1319 isl_basic_set *bset;
1321 bmap = isl_basic_map_copy(bmap);
1322 bset = isl_basic_map_underlying_set(bmap);
1323 bset = isl_basic_set_cow(bset);
1324 bset = isl_basic_set_update_from_tab(bset, tab);
1325 return isl_set_from_basic_set(bset);
1328 /* Wrap the constraints of info->bmap that bound the facet defined
1329 * by inequality "k" around (the opposite of) this inequality to
1330 * include "set". "bound" may be used to store the negated inequality.
1331 * Since the wrapped constraints are not guaranteed to contain the whole
1332 * of info->bmap, we check them in check_wraps.
1333 * If any of the wrapped constraints turn out to be invalid, then
1334 * check_wraps will reset wrap->n_row to zero.
1336 static int add_wraps_around_facet(struct isl_wraps *wraps,
1337 struct isl_coalesce_info *info, int k, isl_int *bound,
1338 __isl_keep isl_set *set)
1340 struct isl_tab_undo *snap;
1341 int n;
1342 unsigned total = isl_basic_map_total_dim(info->bmap);
1344 snap = isl_tab_snap(info->tab);
1346 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1347 return -1;
1348 if (isl_tab_detect_redundant(info->tab) < 0)
1349 return -1;
1351 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1353 n = wraps->mat->n_row;
1354 if (add_wraps(wraps, info, bound, set) < 0)
1355 return -1;
1357 if (isl_tab_rollback(info->tab, snap) < 0)
1358 return -1;
1359 if (check_wraps(wraps->mat, n, info->tab) < 0)
1360 return -1;
1362 return 0;
1365 /* Given a basic set i with a constraint k that is adjacent to
1366 * basic set j, check if we can wrap
1367 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1368 * (always) around their ridges to include the other set.
1369 * If so, replace the pair of basic sets by their union.
1371 * All constraints of i (except k) are assumed to be valid or
1372 * cut constraints for j.
1373 * Wrapping the cut constraints to include basic map j may result
1374 * in constraints that are no longer valid of basic map i
1375 * we have to check that the resulting wrapping constraints are valid for i.
1376 * If "wrap_facet" is not set, then all constraints of i (except k)
1377 * are assumed to be valid for j.
1378 * ____ _____
1379 * / | / \
1380 * / || / |
1381 * \ || => \ |
1382 * \ || \ |
1383 * \___|| \____|
1386 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1387 struct isl_coalesce_info *info, int wrap_facet)
1389 enum isl_change change = isl_change_none;
1390 struct isl_wraps wraps;
1391 isl_ctx *ctx;
1392 isl_mat *mat;
1393 struct isl_set *set_i = NULL;
1394 struct isl_set *set_j = NULL;
1395 struct isl_vec *bound = NULL;
1396 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1398 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1399 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1400 ctx = isl_basic_map_get_ctx(info[i].bmap);
1401 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1402 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1403 1 + total);
1404 wraps_init(&wraps, mat, info, i, j);
1405 bound = isl_vec_alloc(ctx, 1 + total);
1406 if (!set_i || !set_j || !wraps.mat || !bound)
1407 goto error;
1409 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1410 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1412 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1413 wraps.mat->n_row = 1;
1415 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1416 goto error;
1417 if (!wraps.mat->n_row)
1418 goto unbounded;
1420 if (wrap_facet) {
1421 if (add_wraps_around_facet(&wraps, &info[i], k,
1422 bound->el, set_j) < 0)
1423 goto error;
1424 if (!wraps.mat->n_row)
1425 goto unbounded;
1428 change = fuse(i, j, info, wraps.mat, 0, 0);
1430 unbounded:
1431 wraps_free(&wraps);
1433 isl_set_free(set_i);
1434 isl_set_free(set_j);
1436 isl_vec_free(bound);
1438 return change;
1439 error:
1440 wraps_free(&wraps);
1441 isl_vec_free(bound);
1442 isl_set_free(set_i);
1443 isl_set_free(set_j);
1444 return isl_change_error;
1447 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1448 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1449 * add wrapping constraints to wrap.mat for all constraints
1450 * of basic map j that bound the part of basic map j that sticks out
1451 * of the cut constraint.
1452 * "set_i" is the underlying set of basic map i.
1453 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1455 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1456 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1457 * (with respect to the integer points), so we add t(x) >= 0 instead.
1458 * Otherwise, we wrap the constraints of basic map j that are not
1459 * redundant in this intersection and that are not already valid
1460 * for basic map i over basic map i.
1461 * Note that it is sufficient to wrap the constraints to include
1462 * basic map i, because we will only wrap the constraints that do
1463 * not include basic map i already. The wrapped constraint will
1464 * therefore be more relaxed compared to the original constraint.
1465 * Since the original constraint is valid for basic map j, so is
1466 * the wrapped constraint.
1468 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1469 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1470 struct isl_tab_undo *snap)
1472 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1473 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1474 return isl_stat_error;
1475 if (isl_tab_detect_redundant(info_j->tab) < 0)
1476 return isl_stat_error;
1478 if (info_j->tab->empty)
1479 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1480 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1481 return isl_stat_error;
1483 if (isl_tab_rollback(info_j->tab, snap) < 0)
1484 return isl_stat_error;
1486 return isl_stat_ok;
1489 /* Given a pair of basic maps i and j such that j sticks out
1490 * of i at n cut constraints, each time by at most one,
1491 * try to compute wrapping constraints and replace the two
1492 * basic maps by a single basic map.
1493 * The other constraints of i are assumed to be valid for j.
1494 * "set_i" is the underlying set of basic map i.
1495 * "wraps" has been initialized to be of the right size.
1497 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1498 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1499 * of basic map j that bound the part of basic map j that sticks out
1500 * of the cut constraint.
1502 * If any wrapping fails, i.e., if we cannot wrap to touch
1503 * the union, then we give up.
1504 * Otherwise, the pair of basic maps is replaced by their union.
1506 static enum isl_change try_wrap_in_facets(int i, int j,
1507 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1508 __isl_keep isl_set *set_i)
1510 int k, l, w;
1511 unsigned total;
1512 struct isl_tab_undo *snap;
1514 total = isl_basic_map_total_dim(info[i].bmap);
1516 snap = isl_tab_snap(info[j].tab);
1518 wraps->mat->n_row = 0;
1520 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1521 for (l = 0; l < 2; ++l) {
1522 if (info[i].eq[2 * k + l] != STATUS_CUT)
1523 continue;
1524 w = wraps->mat->n_row++;
1525 if (l == 0)
1526 isl_seq_neg(wraps->mat->row[w],
1527 info[i].bmap->eq[k], 1 + total);
1528 else
1529 isl_seq_cpy(wraps->mat->row[w],
1530 info[i].bmap->eq[k], 1 + total);
1531 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1532 return isl_change_error;
1534 if (!wraps->mat->n_row)
1535 return isl_change_none;
1539 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1540 if (info[i].ineq[k] != STATUS_CUT)
1541 continue;
1542 w = wraps->mat->n_row++;
1543 isl_seq_cpy(wraps->mat->row[w],
1544 info[i].bmap->ineq[k], 1 + total);
1545 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1546 return isl_change_error;
1548 if (!wraps->mat->n_row)
1549 return isl_change_none;
1552 return fuse(i, j, info, wraps->mat, 0, 1);
1555 /* Given a pair of basic maps i and j such that j sticks out
1556 * of i at n cut constraints, each time by at most one,
1557 * try to compute wrapping constraints and replace the two
1558 * basic maps by a single basic map.
1559 * The other constraints of i are assumed to be valid for j.
1561 * The core computation is performed by try_wrap_in_facets.
1562 * This function simply extracts an underlying set representation
1563 * of basic map i and initializes the data structure for keeping
1564 * track of wrapping constraints.
1566 static enum isl_change wrap_in_facets(int i, int j, int n,
1567 struct isl_coalesce_info *info)
1569 enum isl_change change = isl_change_none;
1570 struct isl_wraps wraps;
1571 isl_ctx *ctx;
1572 isl_mat *mat;
1573 isl_set *set_i = NULL;
1574 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1575 int max_wrap;
1577 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1578 return isl_change_error;
1580 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1581 max_wrap *= n;
1583 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1584 ctx = isl_basic_map_get_ctx(info[i].bmap);
1585 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1586 wraps_init(&wraps, mat, info, i, j);
1587 if (!set_i || !wraps.mat)
1588 goto error;
1590 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1592 wraps_free(&wraps);
1593 isl_set_free(set_i);
1595 return change;
1596 error:
1597 wraps_free(&wraps);
1598 isl_set_free(set_i);
1599 return isl_change_error;
1602 /* Return the effect of inequality "ineq" on the tableau "tab",
1603 * after relaxing the constant term of "ineq" by one.
1605 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1607 enum isl_ineq_type type;
1609 isl_int_add_ui(ineq[0], ineq[0], 1);
1610 type = isl_tab_ineq_type(tab, ineq);
1611 isl_int_sub_ui(ineq[0], ineq[0], 1);
1613 return type;
1616 /* Given two basic sets i and j,
1617 * check if relaxing all the cut constraints of i by one turns
1618 * them into valid constraint for j and check if we can wrap in
1619 * the bits that are sticking out.
1620 * If so, replace the pair by their union.
1622 * We first check if all relaxed cut inequalities of i are valid for j
1623 * and then try to wrap in the intersections of the relaxed cut inequalities
1624 * with j.
1626 * During this wrapping, we consider the points of j that lie at a distance
1627 * of exactly 1 from i. In particular, we ignore the points that lie in
1628 * between this lower-dimensional space and the basic map i.
1629 * We can therefore only apply this to integer maps.
1630 * ____ _____
1631 * / ___|_ / \
1632 * / | | / |
1633 * \ | | => \ |
1634 * \|____| \ |
1635 * \___| \____/
1637 * _____ ______
1638 * | ____|_ | \
1639 * | | | | |
1640 * | | | => | |
1641 * |_| | | |
1642 * |_____| \______|
1644 * _______
1645 * | |
1646 * | |\ |
1647 * | | \ |
1648 * | | \ |
1649 * | | \|
1650 * | | \
1651 * | |_____\
1652 * | |
1653 * |_______|
1655 * Wrapping can fail if the result of wrapping one of the facets
1656 * around its edges does not produce any new facet constraint.
1657 * In particular, this happens when we try to wrap in unbounded sets.
1659 * _______________________________________________________________________
1661 * | ___
1662 * | | |
1663 * |_| |_________________________________________________________________
1664 * |___|
1666 * The following is not an acceptable result of coalescing the above two
1667 * sets as it includes extra integer points.
1668 * _______________________________________________________________________
1670 * |
1671 * |
1673 * \______________________________________________________________________
1675 static enum isl_change can_wrap_in_set(int i, int j,
1676 struct isl_coalesce_info *info)
1678 int k, l;
1679 int n;
1680 unsigned total;
1682 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1683 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1684 return isl_change_none;
1686 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1687 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1688 if (n == 0)
1689 return isl_change_none;
1691 total = isl_basic_map_total_dim(info[i].bmap);
1692 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1693 for (l = 0; l < 2; ++l) {
1694 enum isl_ineq_type type;
1696 if (info[i].eq[2 * k + l] != STATUS_CUT)
1697 continue;
1699 if (l == 0)
1700 isl_seq_neg(info[i].bmap->eq[k],
1701 info[i].bmap->eq[k], 1 + total);
1702 type = type_of_relaxed(info[j].tab,
1703 info[i].bmap->eq[k]);
1704 if (l == 0)
1705 isl_seq_neg(info[i].bmap->eq[k],
1706 info[i].bmap->eq[k], 1 + total);
1707 if (type == isl_ineq_error)
1708 return isl_change_error;
1709 if (type != isl_ineq_redundant)
1710 return isl_change_none;
1714 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1715 enum isl_ineq_type type;
1717 if (info[i].ineq[k] != STATUS_CUT)
1718 continue;
1720 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1721 if (type == isl_ineq_error)
1722 return isl_change_error;
1723 if (type != isl_ineq_redundant)
1724 return isl_change_none;
1727 return wrap_in_facets(i, j, n, info);
1730 /* Check if either i or j has only cut constraints that can
1731 * be used to wrap in (a facet of) the other basic set.
1732 * if so, replace the pair by their union.
1734 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1736 enum isl_change change = isl_change_none;
1738 change = can_wrap_in_set(i, j, info);
1739 if (change != isl_change_none)
1740 return change;
1742 change = can_wrap_in_set(j, i, info);
1743 return change;
1746 /* Check if all inequality constraints of "i" that cut "j" cease
1747 * to be cut constraints if they are relaxed by one.
1748 * If so, collect the cut constraints in "list".
1749 * The caller is responsible for allocating "list".
1751 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1752 int *list)
1754 int l, n;
1756 n = 0;
1757 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1758 enum isl_ineq_type type;
1760 if (info[i].ineq[l] != STATUS_CUT)
1761 continue;
1762 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1763 if (type == isl_ineq_error)
1764 return isl_bool_error;
1765 if (type != isl_ineq_redundant)
1766 return isl_bool_false;
1767 list[n++] = l;
1770 return isl_bool_true;
1773 /* Given two basic maps such that "j" has at least one equality constraint
1774 * that is adjacent to an inequality constraint of "i" and such that "i" has
1775 * exactly one inequality constraint that is adjacent to an equality
1776 * constraint of "j", check whether "i" can be extended to include "j" or
1777 * whether "j" can be wrapped into "i".
1778 * All remaining constraints of "i" and "j" are assumed to be valid
1779 * or cut constraints of the other basic map.
1780 * However, none of the equality constraints of "i" are cut constraints.
1782 * If "i" has any "cut" inequality constraints, then check if relaxing
1783 * each of them by one is sufficient for them to become valid.
1784 * If so, check if the inequality constraint adjacent to an equality
1785 * constraint of "j" along with all these cut constraints
1786 * can be relaxed by one to contain exactly "j".
1787 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1789 static enum isl_change check_single_adj_eq(int i, int j,
1790 struct isl_coalesce_info *info)
1792 enum isl_change change = isl_change_none;
1793 int k;
1794 int n_cut;
1795 int *relax;
1796 isl_ctx *ctx;
1797 isl_bool try_relax;
1799 n_cut = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1801 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ);
1803 if (n_cut > 0) {
1804 ctx = isl_basic_map_get_ctx(info[i].bmap);
1805 relax = isl_calloc_array(ctx, int, 1 + n_cut);
1806 if (!relax)
1807 return isl_change_error;
1808 relax[0] = k;
1809 try_relax = all_cut_by_one(i, j, info, relax + 1);
1810 if (try_relax < 0)
1811 change = isl_change_error;
1812 } else {
1813 try_relax = isl_bool_true;
1814 relax = &k;
1816 if (try_relax && change == isl_change_none)
1817 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
1818 if (n_cut > 0)
1819 free(relax);
1820 if (change != isl_change_none)
1821 return change;
1823 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
1825 return change;
1828 /* At least one of the basic maps has an equality that is adjacent
1829 * to inequality. Make sure that only one of the basic maps has
1830 * such an equality and that the other basic map has exactly one
1831 * inequality adjacent to an equality.
1832 * If the other basic map does not have such an inequality, then
1833 * check if all its constraints are either valid or cut constraints
1834 * and, if so, try wrapping in the first map into the second.
1835 * Otherwise, try to extend one basic map with the other or
1836 * wrap one basic map in the other.
1838 static enum isl_change check_adj_eq(int i, int j,
1839 struct isl_coalesce_info *info)
1841 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1842 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1843 /* ADJ EQ TOO MANY */
1844 return isl_change_none;
1846 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1847 return check_adj_eq(j, i, info);
1849 /* j has an equality adjacent to an inequality in i */
1851 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1852 if (all_valid_or_cut(&info[i]))
1853 return can_wrap_in_set(i, j, info);
1854 return isl_change_none;
1856 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1857 return isl_change_none;
1858 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1859 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1860 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1861 /* ADJ EQ TOO MANY */
1862 return isl_change_none;
1864 return check_single_adj_eq(i, j, info);
1867 /* The two basic maps lie on adjacent hyperplanes. In particular,
1868 * basic map "i" has an equality that lies parallel to basic map "j".
1869 * Check if we can wrap the facets around the parallel hyperplanes
1870 * to include the other set.
1872 * We perform basically the same operations as can_wrap_in_facet,
1873 * except that we don't need to select a facet of one of the sets.
1875 * \\ \\
1876 * \\ => \\
1877 * \ \|
1879 * If there is more than one equality of "i" adjacent to an equality of "j",
1880 * then the result will satisfy one or more equalities that are a linear
1881 * combination of these equalities. These will be encoded as pairs
1882 * of inequalities in the wrapping constraints and need to be made
1883 * explicit.
1885 static enum isl_change check_eq_adj_eq(int i, int j,
1886 struct isl_coalesce_info *info)
1888 int k;
1889 enum isl_change change = isl_change_none;
1890 int detect_equalities = 0;
1891 struct isl_wraps wraps;
1892 isl_ctx *ctx;
1893 isl_mat *mat;
1894 struct isl_set *set_i = NULL;
1895 struct isl_set *set_j = NULL;
1896 struct isl_vec *bound = NULL;
1897 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1899 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1900 detect_equalities = 1;
1902 k = find(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ);
1904 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1905 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1906 ctx = isl_basic_map_get_ctx(info[i].bmap);
1907 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1908 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1909 1 + total);
1910 wraps_init(&wraps, mat, info, i, j);
1911 bound = isl_vec_alloc(ctx, 1 + total);
1912 if (!set_i || !set_j || !wraps.mat || !bound)
1913 goto error;
1915 if (k % 2 == 0)
1916 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1917 else
1918 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1919 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1921 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1922 wraps.mat->n_row = 1;
1924 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1925 goto error;
1926 if (!wraps.mat->n_row)
1927 goto unbounded;
1929 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1930 isl_seq_neg(bound->el, bound->el, 1 + total);
1932 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1933 wraps.mat->n_row++;
1935 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1936 goto error;
1937 if (!wraps.mat->n_row)
1938 goto unbounded;
1940 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1942 if (0) {
1943 error: change = isl_change_error;
1945 unbounded:
1947 wraps_free(&wraps);
1948 isl_set_free(set_i);
1949 isl_set_free(set_j);
1950 isl_vec_free(bound);
1952 return change;
1955 /* Initialize the "eq" and "ineq" fields of "info".
1957 static void init_status(struct isl_coalesce_info *info)
1959 info->eq = info->ineq = NULL;
1962 /* Set info->eq to the positions of the equalities of info->bmap
1963 * with respect to the basic map represented by "tab".
1964 * If info->eq has already been computed, then do not compute it again.
1966 static void set_eq_status_in(struct isl_coalesce_info *info,
1967 struct isl_tab *tab)
1969 if (info->eq)
1970 return;
1971 info->eq = eq_status_in(info->bmap, tab);
1974 /* Set info->ineq to the positions of the inequalities of info->bmap
1975 * with respect to the basic map represented by "tab".
1976 * If info->ineq has already been computed, then do not compute it again.
1978 static void set_ineq_status_in(struct isl_coalesce_info *info,
1979 struct isl_tab *tab)
1981 if (info->ineq)
1982 return;
1983 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1986 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1987 * This function assumes that init_status has been called on "info" first,
1988 * after which the "eq" and "ineq" fields may or may not have been
1989 * assigned a newly allocated array.
1991 static void clear_status(struct isl_coalesce_info *info)
1993 free(info->eq);
1994 free(info->ineq);
1997 /* Check if the union of the given pair of basic maps
1998 * can be represented by a single basic map.
1999 * If so, replace the pair by the single basic map and return
2000 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2001 * Otherwise, return isl_change_none.
2002 * The two basic maps are assumed to live in the same local space.
2003 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2004 * to have been initialized by the caller, either to NULL or
2005 * to valid information.
2007 * We first check the effect of each constraint of one basic map
2008 * on the other basic map.
2009 * The constraint may be
2010 * redundant the constraint is redundant in its own
2011 * basic map and should be ignore and removed
2012 * in the end
2013 * valid all (integer) points of the other basic map
2014 * satisfy the constraint
2015 * separate no (integer) point of the other basic map
2016 * satisfies the constraint
2017 * cut some but not all points of the other basic map
2018 * satisfy the constraint
2019 * adj_eq the given constraint is adjacent (on the outside)
2020 * to an equality of the other basic map
2021 * adj_ineq the given constraint is adjacent (on the outside)
2022 * to an inequality of the other basic map
2024 * We consider seven cases in which we can replace the pair by a single
2025 * basic map. We ignore all "redundant" constraints.
2027 * 1. all constraints of one basic map are valid
2028 * => the other basic map is a subset and can be removed
2030 * 2. all constraints of both basic maps are either "valid" or "cut"
2031 * and the facets corresponding to the "cut" constraints
2032 * of one of the basic maps lies entirely inside the other basic map
2033 * => the pair can be replaced by a basic map consisting
2034 * of the valid constraints in both basic maps
2036 * 3. there is a single pair of adjacent inequalities
2037 * (all other constraints are "valid")
2038 * => the pair can be replaced by a basic map consisting
2039 * of the valid constraints in both basic maps
2041 * 4. one basic map has a single adjacent inequality, while the other
2042 * constraints are "valid". The other basic map has some
2043 * "cut" constraints, but replacing the adjacent inequality by
2044 * its opposite and adding the valid constraints of the other
2045 * basic map results in a subset of the other basic map
2046 * => the pair can be replaced by a basic map consisting
2047 * of the valid constraints in both basic maps
2049 * 5. there is a single adjacent pair of an inequality and an equality,
2050 * the other constraints of the basic map containing the inequality are
2051 * "valid". Moreover, if the inequality the basic map is relaxed
2052 * and then turned into an equality, then resulting facet lies
2053 * entirely inside the other basic map
2054 * => the pair can be replaced by the basic map containing
2055 * the inequality, with the inequality relaxed.
2057 * 6. there is a single adjacent pair of an inequality and an equality,
2058 * the other constraints of the basic map containing the inequality are
2059 * "valid". Moreover, the facets corresponding to both
2060 * the inequality and the equality can be wrapped around their
2061 * ridges to include the other basic map
2062 * => the pair can be replaced by a basic map consisting
2063 * of the valid constraints in both basic maps together
2064 * with all wrapping constraints
2066 * 7. one of the basic maps extends beyond the other by at most one.
2067 * Moreover, the facets corresponding to the cut constraints and
2068 * the pieces of the other basic map at offset one from these cut
2069 * constraints can be wrapped around their ridges to include
2070 * the union of the two basic maps
2071 * => the pair can be replaced by a basic map consisting
2072 * of the valid constraints in both basic maps together
2073 * with all wrapping constraints
2075 * 8. the two basic maps live in adjacent hyperplanes. In principle
2076 * such sets can always be combined through wrapping, but we impose
2077 * that there is only one such pair, to avoid overeager coalescing.
2079 * Throughout the computation, we maintain a collection of tableaus
2080 * corresponding to the basic maps. When the basic maps are dropped
2081 * or combined, the tableaus are modified accordingly.
2083 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2084 struct isl_coalesce_info *info)
2086 enum isl_change change = isl_change_none;
2088 set_eq_status_in(&info[i], info[j].tab);
2089 if (info[i].bmap->n_eq && !info[i].eq)
2090 goto error;
2091 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
2092 goto error;
2093 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
2094 goto done;
2096 set_eq_status_in(&info[j], info[i].tab);
2097 if (info[j].bmap->n_eq && !info[j].eq)
2098 goto error;
2099 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
2100 goto error;
2101 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
2102 goto done;
2104 set_ineq_status_in(&info[i], info[j].tab);
2105 if (info[i].bmap->n_ineq && !info[i].ineq)
2106 goto error;
2107 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
2108 goto error;
2109 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
2110 goto done;
2112 set_ineq_status_in(&info[j], info[i].tab);
2113 if (info[j].bmap->n_ineq && !info[j].ineq)
2114 goto error;
2115 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2116 goto error;
2117 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2118 goto done;
2120 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2121 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2122 drop(&info[j]);
2123 change = isl_change_drop_second;
2124 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2125 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2126 drop(&info[i]);
2127 change = isl_change_drop_first;
2128 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2129 change = check_eq_adj_eq(i, j, info);
2130 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2131 change = check_eq_adj_eq(j, i, info);
2132 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2133 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2134 change = check_adj_eq(i, j, info);
2135 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2136 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2137 /* Can't happen */
2138 /* BAD ADJ INEQ */
2139 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2140 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2141 change = check_adj_ineq(i, j, info);
2142 } else {
2143 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2144 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2145 change = check_facets(i, j, info);
2146 if (change == isl_change_none)
2147 change = check_wrap(i, j, info);
2150 done:
2151 clear_status(&info[i]);
2152 clear_status(&info[j]);
2153 return change;
2154 error:
2155 clear_status(&info[i]);
2156 clear_status(&info[j]);
2157 return isl_change_error;
2160 /* Check if the union of the given pair of basic maps
2161 * can be represented by a single basic map.
2162 * If so, replace the pair by the single basic map and return
2163 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2164 * Otherwise, return isl_change_none.
2165 * The two basic maps are assumed to live in the same local space.
2167 static enum isl_change coalesce_local_pair(int i, int j,
2168 struct isl_coalesce_info *info)
2170 init_status(&info[i]);
2171 init_status(&info[j]);
2172 return coalesce_local_pair_reuse(i, j, info);
2175 /* Shift the integer division at position "div" of the basic map
2176 * represented by "info" by "shift".
2178 * That is, if the integer division has the form
2180 * floor(f(x)/d)
2182 * then replace it by
2184 * floor((f(x) + shift * d)/d) - shift
2186 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
2188 unsigned total;
2190 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2191 if (!info->bmap)
2192 return -1;
2194 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2195 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2196 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2197 return -1;
2199 return 0;
2202 /* Check if some of the divs in the basic map represented by "info1"
2203 * are shifts of the corresponding divs in the basic map represented
2204 * by "info2". If so, align them with those of "info2".
2205 * Only do this if "info1" and "info2" have the same number
2206 * of integer divisions.
2208 * An integer division is considered to be a shift of another integer
2209 * division if one is equal to the other plus a constant.
2211 * In particular, for each pair of integer divisions, if both are known,
2212 * have identical coefficients (apart from the constant term) and
2213 * if the difference between the constant terms (taking into account
2214 * the denominator) is an integer, then move the difference outside.
2215 * That is, if one integer division is of the form
2217 * floor((f(x) + c_1)/d)
2219 * while the other is of the form
2221 * floor((f(x) + c_2)/d)
2223 * and n = (c_2 - c_1)/d is an integer, then replace the first
2224 * integer division by
2226 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2228 static int harmonize_divs(struct isl_coalesce_info *info1,
2229 struct isl_coalesce_info *info2)
2231 int i;
2232 int total;
2234 if (!info1->bmap || !info2->bmap)
2235 return -1;
2237 if (info1->bmap->n_div != info2->bmap->n_div)
2238 return 0;
2239 if (info1->bmap->n_div == 0)
2240 return 0;
2242 total = isl_basic_map_total_dim(info1->bmap);
2243 for (i = 0; i < info1->bmap->n_div; ++i) {
2244 isl_int d;
2245 int r = 0;
2247 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
2248 isl_int_is_zero(info2->bmap->div[i][0]))
2249 continue;
2250 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2251 continue;
2252 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
2253 continue;
2254 if (!isl_seq_eq(info1->bmap->div[i] + 2,
2255 info2->bmap->div[i] + 2, total))
2256 continue;
2257 isl_int_init(d);
2258 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
2259 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
2260 isl_int_divexact(d, d, info1->bmap->div[i][0]);
2261 r = shift_div(info1, i, d);
2263 isl_int_clear(d);
2264 if (r < 0)
2265 return -1;
2268 return 0;
2271 /* Do the two basic maps live in the same local space, i.e.,
2272 * do they have the same (known) divs?
2273 * If either basic map has any unknown divs, then we can only assume
2274 * that they do not live in the same local space.
2276 static int same_divs(__isl_keep isl_basic_map *bmap1,
2277 __isl_keep isl_basic_map *bmap2)
2279 int i;
2280 int known;
2281 int total;
2283 if (!bmap1 || !bmap2)
2284 return -1;
2285 if (bmap1->n_div != bmap2->n_div)
2286 return 0;
2288 if (bmap1->n_div == 0)
2289 return 1;
2291 known = isl_basic_map_divs_known(bmap1);
2292 if (known < 0 || !known)
2293 return known;
2294 known = isl_basic_map_divs_known(bmap2);
2295 if (known < 0 || !known)
2296 return known;
2298 total = isl_basic_map_total_dim(bmap1);
2299 for (i = 0; i < bmap1->n_div; ++i)
2300 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2301 return 0;
2303 return 1;
2306 /* Assuming that "tab" contains the equality constraints and
2307 * the initial inequality constraints of "bmap", copy the remaining
2308 * inequality constraints of "bmap" to "Tab".
2310 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2312 int i, n_ineq;
2314 if (!bmap)
2315 return isl_stat_error;
2317 n_ineq = tab->n_con - tab->n_eq;
2318 for (i = n_ineq; i < bmap->n_ineq; ++i)
2319 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2320 return isl_stat_error;
2322 return isl_stat_ok;
2325 /* Description of an integer division that is added
2326 * during an expansion.
2327 * "pos" is the position of the corresponding variable.
2328 * "cst" indicates whether this integer division has a fixed value.
2329 * "val" contains the fixed value, if the value is fixed.
2331 struct isl_expanded {
2332 int pos;
2333 isl_bool cst;
2334 isl_int val;
2337 /* For each of the "n" integer division variables "expanded",
2338 * if the variable has a fixed value, then add two inequality
2339 * constraints expressing the fixed value.
2340 * Otherwise, add the corresponding div constraints.
2341 * The caller is responsible for removing the div constraints
2342 * that it added for all these "n" integer divisions.
2344 * The div constraints and the pair of inequality constraints
2345 * forcing the fixed value cannot both be added for a given variable
2346 * as the combination may render some of the original constraints redundant.
2347 * These would then be ignored during the coalescing detection,
2348 * while they could remain in the fused result.
2350 * The two added inequality constraints are
2352 * -a + v >= 0
2353 * a - v >= 0
2355 * with "a" the variable and "v" its fixed value.
2356 * The facet corresponding to one of these two constraints is selected
2357 * in the tableau to ensure that the pair of inequality constraints
2358 * is treated as an equality constraint.
2360 * The information in info->ineq is thrown away because it was
2361 * computed in terms of div constraints, while some of those
2362 * have now been replaced by these pairs of inequality constraints.
2364 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2365 int n, struct isl_expanded *expanded)
2367 unsigned o_div;
2368 int i;
2369 isl_vec *ineq;
2371 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2372 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2373 if (!ineq)
2374 return isl_stat_error;
2375 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2377 for (i = 0; i < n; ++i) {
2378 if (!expanded[i].cst) {
2379 info->bmap = isl_basic_map_extend_constraints(
2380 info->bmap, 0, 2);
2381 if (isl_basic_map_add_div_constraints(info->bmap,
2382 expanded[i].pos - o_div) < 0)
2383 break;
2384 } else {
2385 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2386 isl_int_set(ineq->el[0], expanded[i].val);
2387 info->bmap = isl_basic_map_add_ineq(info->bmap,
2388 ineq->el);
2389 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2390 isl_int_neg(ineq->el[0], expanded[i].val);
2391 info->bmap = isl_basic_map_add_ineq(info->bmap,
2392 ineq->el);
2393 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2395 if (copy_ineq(info->tab, info->bmap) < 0)
2396 break;
2397 if (expanded[i].cst &&
2398 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2399 break;
2402 isl_vec_free(ineq);
2404 clear_status(info);
2405 init_status(info);
2407 return i < n ? isl_stat_error : isl_stat_ok;
2410 /* Insert the "n" integer division variables "expanded"
2411 * into info->tab and info->bmap and
2412 * update info->ineq with respect to the redundant constraints
2413 * in the resulting tableau.
2414 * "bmap" contains the result of this insertion in info->bmap,
2415 * while info->bmap is the original version
2416 * of "bmap", i.e., the one that corresponds to the current
2417 * state of info->tab. The number of constraints in info->bmap
2418 * is assumed to be the same as the number of constraints
2419 * in info->tab. This is required to be able to detect
2420 * the extra constraints in "bmap".
2422 * In particular, introduce extra variables corresponding
2423 * to the extra integer divisions and add the div constraints
2424 * that were added to "bmap" after info->tab was created
2425 * from info->bmap.
2426 * Furthermore, check if these extra integer divisions happen
2427 * to attain a fixed integer value in info->tab.
2428 * If so, replace the corresponding div constraints by pairs
2429 * of inequality constraints that fix these
2430 * integer divisions to their single integer values.
2431 * Replace info->bmap by "bmap" to match the changes to info->tab.
2432 * info->ineq was computed without a tableau and therefore
2433 * does not take into account the redundant constraints
2434 * in the tableau. Mark them here.
2435 * There is no need to check the newly added div constraints
2436 * since they cannot be redundant.
2437 * The redundancy check is not performed when constants have been discovered
2438 * since info->ineq is completely thrown away in this case.
2440 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2441 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2443 int i, n_ineq;
2444 unsigned n_eq;
2445 struct isl_tab_undo *snap;
2446 int any;
2448 if (!bmap)
2449 return isl_stat_error;
2450 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2451 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2452 "original tableau does not correspond "
2453 "to original basic map", goto error);
2455 if (isl_tab_extend_vars(info->tab, n) < 0)
2456 goto error;
2457 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2458 goto error;
2460 for (i = 0; i < n; ++i) {
2461 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2462 goto error;
2465 snap = isl_tab_snap(info->tab);
2467 n_ineq = info->tab->n_con - info->tab->n_eq;
2468 if (copy_ineq(info->tab, bmap) < 0)
2469 goto error;
2471 isl_basic_map_free(info->bmap);
2472 info->bmap = bmap;
2474 any = 0;
2475 for (i = 0; i < n; ++i) {
2476 expanded[i].cst = isl_tab_is_constant(info->tab,
2477 expanded[i].pos, &expanded[i].val);
2478 if (expanded[i].cst < 0)
2479 return isl_stat_error;
2480 if (expanded[i].cst)
2481 any = 1;
2484 if (any) {
2485 if (isl_tab_rollback(info->tab, snap) < 0)
2486 return isl_stat_error;
2487 info->bmap = isl_basic_map_cow(info->bmap);
2488 if (isl_basic_map_free_inequality(info->bmap, 2 * n) < 0)
2489 return isl_stat_error;
2491 return fix_constant_divs(info, n, expanded);
2494 n_eq = info->bmap->n_eq;
2495 for (i = 0; i < n_ineq; ++i) {
2496 if (isl_tab_is_redundant(info->tab, n_eq + i))
2497 info->ineq[i] = STATUS_REDUNDANT;
2500 return isl_stat_ok;
2501 error:
2502 isl_basic_map_free(bmap);
2503 return isl_stat_error;
2506 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2507 * in isl_basic_map_expand_divs using the expansion "exp" and
2508 * update info->ineq with respect to the redundant constraints
2509 * in the resulting tableau. info->bmap is the original version
2510 * of "bmap", i.e., the one that corresponds to the current
2511 * state of info->tab. The number of constraints in info->bmap
2512 * is assumed to be the same as the number of constraints
2513 * in info->tab. This is required to be able to detect
2514 * the extra constraints in "bmap".
2516 * Extract the positions where extra local variables are introduced
2517 * from "exp" and call tab_insert_divs.
2519 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2520 __isl_take isl_basic_map *bmap)
2522 isl_ctx *ctx;
2523 struct isl_expanded *expanded;
2524 int i, j, k, n;
2525 int extra_var;
2526 unsigned total, pos, n_div;
2527 isl_stat r;
2529 total = isl_basic_map_dim(bmap, isl_dim_all);
2530 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2531 pos = total - n_div;
2532 extra_var = total - info->tab->n_var;
2533 n = n_div - extra_var;
2535 ctx = isl_basic_map_get_ctx(bmap);
2536 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
2537 if (extra_var && !expanded)
2538 goto error;
2540 i = 0;
2541 k = 0;
2542 for (j = 0; j < n_div; ++j) {
2543 if (i < n && exp[i] == j) {
2544 ++i;
2545 continue;
2547 expanded[k++].pos = pos + j;
2550 for (k = 0; k < extra_var; ++k)
2551 isl_int_init(expanded[k].val);
2553 r = tab_insert_divs(info, extra_var, expanded, bmap);
2555 for (k = 0; k < extra_var; ++k)
2556 isl_int_clear(expanded[k].val);
2557 free(expanded);
2559 return r;
2560 error:
2561 isl_basic_map_free(bmap);
2562 return isl_stat_error;
2565 /* Check if the union of the basic maps represented by info[i] and info[j]
2566 * can be represented by a single basic map,
2567 * after expanding the divs of info[i] to match those of info[j].
2568 * If so, replace the pair by the single basic map and return
2569 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2570 * Otherwise, return isl_change_none.
2572 * The caller has already checked for info[j] being a subset of info[i].
2573 * If some of the divs of info[j] are unknown, then the expanded info[i]
2574 * will not have the corresponding div constraints. The other patterns
2575 * therefore cannot apply. Skip the computation in this case.
2577 * The expansion is performed using the divs "div" and expansion "exp"
2578 * computed by the caller.
2579 * info[i].bmap has already been expanded and the result is passed in
2580 * as "bmap".
2581 * The "eq" and "ineq" fields of info[i] reflect the status of
2582 * the constraints of the expanded "bmap" with respect to info[j].tab.
2583 * However, inequality constraints that are redundant in info[i].tab
2584 * have not yet been marked as such because no tableau was available.
2586 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2587 * updating info[i].ineq with respect to the redundant constraints.
2588 * Then try and coalesce the expanded info[i] with info[j],
2589 * reusing the information in info[i].eq and info[i].ineq.
2590 * If this does not result in any coalescing or if it results in info[j]
2591 * getting dropped (which should not happen in practice, since the case
2592 * of info[j] being a subset of info[i] has already been checked by
2593 * the caller), then revert info[i] to its original state.
2595 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2596 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2597 int *exp)
2599 isl_bool known;
2600 isl_basic_map *bmap_i;
2601 struct isl_tab_undo *snap;
2602 enum isl_change change = isl_change_none;
2604 known = isl_basic_map_divs_known(info[j].bmap);
2605 if (known < 0 || !known) {
2606 clear_status(&info[i]);
2607 isl_basic_map_free(bmap);
2608 return known < 0 ? isl_change_error : isl_change_none;
2611 bmap_i = isl_basic_map_copy(info[i].bmap);
2612 snap = isl_tab_snap(info[i].tab);
2613 if (expand_tab(&info[i], exp, bmap) < 0)
2614 change = isl_change_error;
2616 init_status(&info[j]);
2617 if (change == isl_change_none)
2618 change = coalesce_local_pair_reuse(i, j, info);
2619 else
2620 clear_status(&info[i]);
2621 if (change != isl_change_none && change != isl_change_drop_second) {
2622 isl_basic_map_free(bmap_i);
2623 } else {
2624 isl_basic_map_free(info[i].bmap);
2625 info[i].bmap = bmap_i;
2627 if (isl_tab_rollback(info[i].tab, snap) < 0)
2628 change = isl_change_error;
2631 return change;
2634 /* Check if the union of "bmap" and the basic map represented by info[j]
2635 * can be represented by a single basic map,
2636 * after expanding the divs of "bmap" to match those of info[j].
2637 * If so, replace the pair by the single basic map and return
2638 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2639 * Otherwise, return isl_change_none.
2641 * In particular, check if the expanded "bmap" contains the basic map
2642 * represented by the tableau info[j].tab.
2643 * The expansion is performed using the divs "div" and expansion "exp"
2644 * computed by the caller.
2645 * Then we check if all constraints of the expanded "bmap" are valid for
2646 * info[j].tab.
2648 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2649 * In this case, the positions of the constraints of info[i].bmap
2650 * with respect to the basic map represented by info[j] are stored
2651 * in info[i].
2653 * If the expanded "bmap" does not contain the basic map
2654 * represented by the tableau info[j].tab and if "i" is not -1,
2655 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2656 * as well and check if that results in coalescing.
2658 static enum isl_change coalesce_with_expanded_divs(
2659 __isl_keep isl_basic_map *bmap, int i, int j,
2660 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2662 enum isl_change change = isl_change_none;
2663 struct isl_coalesce_info info_local, *info_i;
2665 info_i = i >= 0 ? &info[i] : &info_local;
2666 init_status(info_i);
2667 bmap = isl_basic_map_copy(bmap);
2668 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2669 bmap = isl_basic_map_mark_final(bmap);
2671 if (!bmap)
2672 goto error;
2674 info_i->eq = eq_status_in(bmap, info[j].tab);
2675 if (bmap->n_eq && !info_i->eq)
2676 goto error;
2677 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2678 goto error;
2679 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2680 goto done;
2682 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2683 if (bmap->n_ineq && !info_i->ineq)
2684 goto error;
2685 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2686 goto error;
2687 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2688 goto done;
2690 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2691 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2692 drop(&info[j]);
2693 change = isl_change_drop_second;
2696 if (change == isl_change_none && i != -1)
2697 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2699 done:
2700 isl_basic_map_free(bmap);
2701 clear_status(info_i);
2702 return change;
2703 error:
2704 isl_basic_map_free(bmap);
2705 clear_status(info_i);
2706 return isl_change_error;
2709 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2710 * can be represented by a single basic map,
2711 * after aligning the divs of "bmap_i" to match those of info[j].
2712 * If so, replace the pair by the single basic map and return
2713 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2714 * Otherwise, return isl_change_none.
2716 * In particular, check if "bmap_i" contains the basic map represented by
2717 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2718 * Note that this can only succeed if the number of divs of "bmap_i"
2719 * is smaller than (or equal to) the number of divs of info[j].
2721 * We first check if the divs of "bmap_i" are all known and form a subset
2722 * of those of info[j].bmap. If so, we pass control over to
2723 * coalesce_with_expanded_divs.
2725 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2727 static enum isl_change coalesce_after_aligning_divs(
2728 __isl_keep isl_basic_map *bmap_i, int i, int j,
2729 struct isl_coalesce_info *info)
2731 int known;
2732 isl_mat *div_i, *div_j, *div;
2733 int *exp1 = NULL;
2734 int *exp2 = NULL;
2735 isl_ctx *ctx;
2736 enum isl_change change;
2738 known = isl_basic_map_divs_known(bmap_i);
2739 if (known < 0 || !known)
2740 return known;
2742 ctx = isl_basic_map_get_ctx(bmap_i);
2744 div_i = isl_basic_map_get_divs(bmap_i);
2745 div_j = isl_basic_map_get_divs(info[j].bmap);
2747 if (!div_i || !div_j)
2748 goto error;
2750 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2751 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2752 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2753 goto error;
2755 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2756 if (!div)
2757 goto error;
2759 if (div->n_row == div_j->n_row)
2760 change = coalesce_with_expanded_divs(bmap_i,
2761 i, j, info, div, exp1);
2762 else
2763 change = isl_change_none;
2765 isl_mat_free(div);
2767 isl_mat_free(div_i);
2768 isl_mat_free(div_j);
2770 free(exp2);
2771 free(exp1);
2773 return change;
2774 error:
2775 isl_mat_free(div_i);
2776 isl_mat_free(div_j);
2777 free(exp1);
2778 free(exp2);
2779 return isl_change_error;
2782 /* Check if basic map "j" is a subset of basic map "i" after
2783 * exploiting the extra equalities of "j" to simplify the divs of "i".
2784 * If so, remove basic map "j" and return isl_change_drop_second.
2786 * If "j" does not have any equalities or if they are the same
2787 * as those of "i", then we cannot exploit them to simplify the divs.
2788 * Similarly, if there are no divs in "i", then they cannot be simplified.
2789 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2790 * then "j" cannot be a subset of "i".
2792 * Otherwise, we intersect "i" with the affine hull of "j" and then
2793 * check if "j" is a subset of the result after aligning the divs.
2794 * If so, then "j" is definitely a subset of "i" and can be removed.
2795 * Note that if after intersection with the affine hull of "j".
2796 * "i" still has more divs than "j", then there is no way we can
2797 * align the divs of "i" to those of "j".
2799 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2800 struct isl_coalesce_info *info)
2802 isl_basic_map *hull_i, *hull_j, *bmap_i;
2803 int equal, empty;
2804 enum isl_change change;
2806 if (info[j].bmap->n_eq == 0)
2807 return isl_change_none;
2808 if (info[i].bmap->n_div == 0)
2809 return isl_change_none;
2811 hull_i = isl_basic_map_copy(info[i].bmap);
2812 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2813 hull_j = isl_basic_map_copy(info[j].bmap);
2814 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2816 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2817 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2818 empty = isl_basic_map_plain_is_empty(hull_j);
2819 isl_basic_map_free(hull_i);
2821 if (equal < 0 || equal || empty < 0 || empty) {
2822 isl_basic_map_free(hull_j);
2823 if (equal < 0 || empty < 0)
2824 return isl_change_error;
2825 return isl_change_none;
2828 bmap_i = isl_basic_map_copy(info[i].bmap);
2829 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2830 if (!bmap_i)
2831 return isl_change_error;
2833 if (bmap_i->n_div > info[j].bmap->n_div) {
2834 isl_basic_map_free(bmap_i);
2835 return isl_change_none;
2838 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2840 isl_basic_map_free(bmap_i);
2842 return change;
2845 /* Check if the union of and the basic maps represented by info[i] and info[j]
2846 * can be represented by a single basic map, by aligning or equating
2847 * their integer divisions.
2848 * If so, replace the pair by the single basic map and return
2849 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2850 * Otherwise, return isl_change_none.
2852 * Note that we only perform any test if the number of divs is different
2853 * in the two basic maps. In case the number of divs is the same,
2854 * we have already established that the divs are different
2855 * in the two basic maps.
2856 * In particular, if the number of divs of basic map i is smaller than
2857 * the number of divs of basic map j, then we check if j is a subset of i
2858 * and vice versa.
2860 static enum isl_change coalesce_divs(int i, int j,
2861 struct isl_coalesce_info *info)
2863 enum isl_change change = isl_change_none;
2865 if (info[i].bmap->n_div < info[j].bmap->n_div)
2866 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2867 if (change != isl_change_none)
2868 return change;
2870 if (info[j].bmap->n_div < info[i].bmap->n_div)
2871 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2872 if (change != isl_change_none)
2873 return invert_change(change);
2875 change = coalesce_subset_with_equalities(i, j, info);
2876 if (change != isl_change_none)
2877 return change;
2879 change = coalesce_subset_with_equalities(j, i, info);
2880 if (change != isl_change_none)
2881 return invert_change(change);
2883 return isl_change_none;
2886 /* Does "bmap" involve any divs that themselves refer to divs?
2888 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2890 int i;
2891 unsigned total;
2892 unsigned n_div;
2894 total = isl_basic_map_dim(bmap, isl_dim_all);
2895 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2896 total -= n_div;
2898 for (i = 0; i < n_div; ++i)
2899 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2900 n_div) != -1)
2901 return 1;
2903 return 0;
2906 /* Return a list of affine expressions, one for each integer division
2907 * in "bmap_i". For each integer division that also appears in "bmap_j",
2908 * the affine expression is set to NaN. The number of NaNs in the list
2909 * is equal to the number of integer divisions in "bmap_j".
2910 * For the other integer divisions of "bmap_i", the corresponding
2911 * element in the list is a purely affine expression equal to the integer
2912 * division in "hull".
2913 * If no such list can be constructed, then the number of elements
2914 * in the returned list is smaller than the number of integer divisions
2915 * in "bmap_i".
2917 static __isl_give isl_aff_list *set_up_substitutions(
2918 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2919 __isl_take isl_basic_map *hull)
2921 unsigned n_div_i, n_div_j, total;
2922 isl_ctx *ctx;
2923 isl_local_space *ls;
2924 isl_basic_set *wrap_hull;
2925 isl_aff *aff_nan;
2926 isl_aff_list *list;
2927 int i, j;
2929 if (!hull)
2930 return NULL;
2932 ctx = isl_basic_map_get_ctx(hull);
2934 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2935 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2936 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2938 ls = isl_basic_map_get_local_space(bmap_i);
2939 ls = isl_local_space_wrap(ls);
2940 wrap_hull = isl_basic_map_wrap(hull);
2942 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2943 list = isl_aff_list_alloc(ctx, n_div_i);
2945 j = 0;
2946 for (i = 0; i < n_div_i; ++i) {
2947 isl_aff *aff;
2949 if (j < n_div_j &&
2950 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2951 ++j;
2952 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2953 continue;
2955 if (n_div_i - i <= n_div_j - j)
2956 break;
2958 aff = isl_local_space_get_div(ls, i);
2959 aff = isl_aff_substitute_equalities(aff,
2960 isl_basic_set_copy(wrap_hull));
2961 aff = isl_aff_floor(aff);
2962 if (!aff)
2963 goto error;
2964 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2965 isl_aff_free(aff);
2966 break;
2969 list = isl_aff_list_add(list, aff);
2972 isl_aff_free(aff_nan);
2973 isl_local_space_free(ls);
2974 isl_basic_set_free(wrap_hull);
2976 return list;
2977 error:
2978 isl_aff_free(aff_nan);
2979 isl_local_space_free(ls);
2980 isl_basic_set_free(wrap_hull);
2981 isl_aff_list_free(list);
2982 return NULL;
2985 /* Add variables to info->bmap and info->tab corresponding to the elements
2986 * in "list" that are not set to NaN.
2987 * "extra_var" is the number of these elements.
2988 * "dim" is the offset in the variables of "tab" where we should
2989 * start considering the elements in "list".
2990 * When this function returns, the total number of variables in "tab"
2991 * is equal to "dim" plus the number of elements in "list".
2993 * The newly added existentially quantified variables are not given
2994 * an explicit representation because the corresponding div constraints
2995 * do not appear in info->bmap. These constraints are not added
2996 * to info->bmap because for internal consistency, they would need to
2997 * be added to info->tab as well, where they could combine with the equality
2998 * that is added later to result in constraints that do not hold
2999 * in the original input.
3001 static int add_sub_vars(struct isl_coalesce_info *info,
3002 __isl_keep isl_aff_list *list, int dim, int extra_var)
3004 int i, j, n, d;
3005 isl_space *space;
3007 space = isl_basic_map_get_space(info->bmap);
3008 info->bmap = isl_basic_map_cow(info->bmap);
3009 info->bmap = isl_basic_map_extend_space(info->bmap, space,
3010 extra_var, 0, 0);
3011 if (!info->bmap)
3012 return -1;
3013 n = isl_aff_list_n_aff(list);
3014 for (i = 0; i < n; ++i) {
3015 int is_nan;
3016 isl_aff *aff;
3018 aff = isl_aff_list_get_aff(list, i);
3019 is_nan = isl_aff_is_nan(aff);
3020 isl_aff_free(aff);
3021 if (is_nan < 0)
3022 return -1;
3023 if (is_nan)
3024 continue;
3026 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3027 return -1;
3028 d = isl_basic_map_alloc_div(info->bmap);
3029 if (d < 0)
3030 return -1;
3031 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3032 if (!info->bmap)
3033 return -1;
3034 for (j = d; j > i; --j)
3035 isl_basic_map_swap_div(info->bmap, j - 1, j);
3038 return 0;
3041 /* For each element in "list" that is not set to NaN, fix the corresponding
3042 * variable in "tab" to the purely affine expression defined by the element.
3043 * "dim" is the offset in the variables of "tab" where we should
3044 * start considering the elements in "list".
3046 * This function assumes that a sufficient number of rows and
3047 * elements in the constraint array are available in the tableau.
3049 static int add_sub_equalities(struct isl_tab *tab,
3050 __isl_keep isl_aff_list *list, int dim)
3052 int i, n;
3053 isl_ctx *ctx;
3054 isl_vec *sub;
3055 isl_aff *aff;
3057 n = isl_aff_list_n_aff(list);
3059 ctx = isl_tab_get_ctx(tab);
3060 sub = isl_vec_alloc(ctx, 1 + dim + n);
3061 if (!sub)
3062 return -1;
3063 isl_seq_clr(sub->el + 1 + dim, n);
3065 for (i = 0; i < n; ++i) {
3066 aff = isl_aff_list_get_aff(list, i);
3067 if (!aff)
3068 goto error;
3069 if (isl_aff_is_nan(aff)) {
3070 isl_aff_free(aff);
3071 continue;
3073 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3074 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3075 if (isl_tab_add_eq(tab, sub->el) < 0)
3076 goto error;
3077 isl_int_set_si(sub->el[1 + dim + i], 0);
3078 isl_aff_free(aff);
3081 isl_vec_free(sub);
3082 return 0;
3083 error:
3084 isl_aff_free(aff);
3085 isl_vec_free(sub);
3086 return -1;
3089 /* Add variables to info->tab and info->bmap corresponding to the elements
3090 * in "list" that are not set to NaN. The value of the added variable
3091 * in info->tab is fixed to the purely affine expression defined by the element.
3092 * "dim" is the offset in the variables of info->tab where we should
3093 * start considering the elements in "list".
3094 * When this function returns, the total number of variables in info->tab
3095 * is equal to "dim" plus the number of elements in "list".
3097 static int add_subs(struct isl_coalesce_info *info,
3098 __isl_keep isl_aff_list *list, int dim)
3100 int extra_var;
3101 int n;
3103 if (!list)
3104 return -1;
3106 n = isl_aff_list_n_aff(list);
3107 extra_var = n - (info->tab->n_var - dim);
3109 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3110 return -1;
3111 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3112 return -1;
3113 if (add_sub_vars(info, list, dim, extra_var) < 0)
3114 return -1;
3116 return add_sub_equalities(info->tab, list, dim);
3119 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3120 * divisions in "i" but not in "j" to basic map "j", with values
3121 * specified by "list". The total number of elements in "list"
3122 * is equal to the number of integer divisions in "i", while the number
3123 * of NaN elements in the list is equal to the number of integer divisions
3124 * in "j".
3126 * If no coalescing can be performed, then we need to revert basic map "j"
3127 * to its original state. We do the same if basic map "i" gets dropped
3128 * during the coalescing, even though this should not happen in practice
3129 * since we have already checked for "j" being a subset of "i"
3130 * before we reach this stage.
3132 static enum isl_change coalesce_with_subs(int i, int j,
3133 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3135 isl_basic_map *bmap_j;
3136 struct isl_tab_undo *snap;
3137 unsigned dim;
3138 enum isl_change change;
3140 bmap_j = isl_basic_map_copy(info[j].bmap);
3141 snap = isl_tab_snap(info[j].tab);
3143 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3144 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
3145 if (add_subs(&info[j], list, dim) < 0)
3146 goto error;
3148 change = coalesce_local_pair(i, j, info);
3149 if (change != isl_change_none && change != isl_change_drop_first) {
3150 isl_basic_map_free(bmap_j);
3151 } else {
3152 isl_basic_map_free(info[j].bmap);
3153 info[j].bmap = bmap_j;
3155 if (isl_tab_rollback(info[j].tab, snap) < 0)
3156 return isl_change_error;
3159 return change;
3160 error:
3161 isl_basic_map_free(bmap_j);
3162 return isl_change_error;
3165 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3166 * those extra integer divisions in "i" that can be simplified away
3167 * using the extra equalities in "j".
3168 * All divs are assumed to be known and not contain any nested divs.
3170 * We first check if there are any extra equalities in "j" that we
3171 * can exploit. Then we check if every integer division in "i"
3172 * either already appears in "j" or can be simplified using the
3173 * extra equalities to a purely affine expression.
3174 * If these tests succeed, then we try to coalesce the two basic maps
3175 * by introducing extra dimensions in "j" corresponding to
3176 * the extra integer divsisions "i" fixed to the corresponding
3177 * purely affine expression.
3179 static enum isl_change check_coalesce_into_eq(int i, int j,
3180 struct isl_coalesce_info *info)
3182 unsigned n_div_i, n_div_j;
3183 isl_basic_map *hull_i, *hull_j;
3184 int equal, empty;
3185 isl_aff_list *list;
3186 enum isl_change change;
3188 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3189 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3190 if (n_div_i <= n_div_j)
3191 return isl_change_none;
3192 if (info[j].bmap->n_eq == 0)
3193 return isl_change_none;
3195 hull_i = isl_basic_map_copy(info[i].bmap);
3196 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3197 hull_j = isl_basic_map_copy(info[j].bmap);
3198 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3200 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3201 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3202 empty = isl_basic_map_plain_is_empty(hull_j);
3203 isl_basic_map_free(hull_i);
3205 if (equal < 0 || empty < 0)
3206 goto error;
3207 if (equal || empty) {
3208 isl_basic_map_free(hull_j);
3209 return isl_change_none;
3212 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3213 if (!list)
3214 return isl_change_error;
3215 if (isl_aff_list_n_aff(list) < n_div_i)
3216 change = isl_change_none;
3217 else
3218 change = coalesce_with_subs(i, j, info, list);
3220 isl_aff_list_free(list);
3222 return change;
3223 error:
3224 isl_basic_map_free(hull_j);
3225 return isl_change_error;
3228 /* Check if we can coalesce basic maps "i" and "j" after copying
3229 * those extra integer divisions in one of the basic maps that can
3230 * be simplified away using the extra equalities in the other basic map.
3231 * We require all divs to be known in both basic maps.
3232 * Furthermore, to simplify the comparison of div expressions,
3233 * we do not allow any nested integer divisions.
3235 static enum isl_change check_coalesce_eq(int i, int j,
3236 struct isl_coalesce_info *info)
3238 int known, nested;
3239 enum isl_change change;
3241 known = isl_basic_map_divs_known(info[i].bmap);
3242 if (known < 0 || !known)
3243 return known < 0 ? isl_change_error : isl_change_none;
3244 known = isl_basic_map_divs_known(info[j].bmap);
3245 if (known < 0 || !known)
3246 return known < 0 ? isl_change_error : isl_change_none;
3247 nested = has_nested_div(info[i].bmap);
3248 if (nested < 0 || nested)
3249 return nested < 0 ? isl_change_error : isl_change_none;
3250 nested = has_nested_div(info[j].bmap);
3251 if (nested < 0 || nested)
3252 return nested < 0 ? isl_change_error : isl_change_none;
3254 change = check_coalesce_into_eq(i, j, info);
3255 if (change != isl_change_none)
3256 return change;
3257 change = check_coalesce_into_eq(j, i, info);
3258 if (change != isl_change_none)
3259 return invert_change(change);
3261 return isl_change_none;
3264 /* Check if the union of the given pair of basic maps
3265 * can be represented by a single basic map.
3266 * If so, replace the pair by the single basic map and return
3267 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3268 * Otherwise, return isl_change_none.
3270 * We first check if the two basic maps live in the same local space,
3271 * after aligning the divs that differ by only an integer constant.
3272 * If so, we do the complete check. Otherwise, we check if they have
3273 * the same number of integer divisions and can be coalesced, if one is
3274 * an obvious subset of the other or if the extra integer divisions
3275 * of one basic map can be simplified away using the extra equalities
3276 * of the other basic map.
3278 static enum isl_change coalesce_pair(int i, int j,
3279 struct isl_coalesce_info *info)
3281 int same;
3282 enum isl_change change;
3284 if (harmonize_divs(&info[i], &info[j]) < 0)
3285 return isl_change_error;
3286 same = same_divs(info[i].bmap, info[j].bmap);
3287 if (same < 0)
3288 return isl_change_error;
3289 if (same)
3290 return coalesce_local_pair(i, j, info);
3292 if (info[i].bmap->n_div == info[j].bmap->n_div) {
3293 change = coalesce_local_pair(i, j, info);
3294 if (change != isl_change_none)
3295 return change;
3298 change = coalesce_divs(i, j, info);
3299 if (change != isl_change_none)
3300 return change;
3302 return check_coalesce_eq(i, j, info);
3305 /* Return the maximum of "a" and "b".
3307 static int isl_max(int a, int b)
3309 return a > b ? a : b;
3312 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3313 * with those in the range [start2, end2[, skipping basic maps
3314 * that have been removed (either before or within this function).
3316 * For each basic map i in the first range, we check if it can be coalesced
3317 * with respect to any previously considered basic map j in the second range.
3318 * If i gets dropped (because it was a subset of some j), then
3319 * we can move on to the next basic map.
3320 * If j gets dropped, we need to continue checking against the other
3321 * previously considered basic maps.
3322 * If the two basic maps got fused, then we recheck the fused basic map
3323 * against the previously considered basic maps, starting at i + 1
3324 * (even if start2 is greater than i + 1).
3326 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3327 int start1, int end1, int start2, int end2)
3329 int i, j;
3331 for (i = end1 - 1; i >= start1; --i) {
3332 if (info[i].removed)
3333 continue;
3334 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3335 enum isl_change changed;
3337 if (info[j].removed)
3338 continue;
3339 if (info[i].removed)
3340 isl_die(ctx, isl_error_internal,
3341 "basic map unexpectedly removed",
3342 return -1);
3343 changed = coalesce_pair(i, j, info);
3344 switch (changed) {
3345 case isl_change_error:
3346 return -1;
3347 case isl_change_none:
3348 case isl_change_drop_second:
3349 continue;
3350 case isl_change_drop_first:
3351 j = end2;
3352 break;
3353 case isl_change_fuse:
3354 j = i;
3355 break;
3360 return 0;
3363 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3365 * We consider groups of basic maps that live in the same apparent
3366 * affine hull and we first coalesce within such a group before we
3367 * coalesce the elements in the group with elements of previously
3368 * considered groups. If a fuse happens during the second phase,
3369 * then we also reconsider the elements within the group.
3371 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3373 int start, end;
3375 for (end = n; end > 0; end = start) {
3376 start = end - 1;
3377 while (start >= 1 &&
3378 info[start - 1].hull_hash == info[start].hull_hash)
3379 start--;
3380 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3381 return -1;
3382 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3383 return -1;
3386 return 0;
3389 /* Update the basic maps in "map" based on the information in "info".
3390 * In particular, remove the basic maps that have been marked removed and
3391 * update the others based on the information in the corresponding tableau.
3392 * Since we detected implicit equalities without calling
3393 * isl_basic_map_gauss, we need to do it now.
3394 * Also call isl_basic_map_simplify if we may have lost the definition
3395 * of one or more integer divisions.
3397 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3398 int n, struct isl_coalesce_info *info)
3400 int i;
3402 if (!map)
3403 return NULL;
3405 for (i = n - 1; i >= 0; --i) {
3406 if (info[i].removed) {
3407 isl_basic_map_free(map->p[i]);
3408 if (i != map->n - 1)
3409 map->p[i] = map->p[map->n - 1];
3410 map->n--;
3411 continue;
3414 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3415 info[i].tab);
3416 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3417 if (info[i].simplify)
3418 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3419 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3420 if (!info[i].bmap)
3421 return isl_map_free(map);
3422 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3423 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3424 isl_basic_map_free(map->p[i]);
3425 map->p[i] = info[i].bmap;
3426 info[i].bmap = NULL;
3429 return map;
3432 /* For each pair of basic maps in the map, check if the union of the two
3433 * can be represented by a single basic map.
3434 * If so, replace the pair by the single basic map and start over.
3436 * We factor out any (hidden) common factor from the constraint
3437 * coefficients to improve the detection of adjacent constraints.
3439 * Since we are constructing the tableaus of the basic maps anyway,
3440 * we exploit them to detect implicit equalities and redundant constraints.
3441 * This also helps the coalescing as it can ignore the redundant constraints.
3442 * In order to avoid confusion, we make all implicit equalities explicit
3443 * in the basic maps. We don't call isl_basic_map_gauss, though,
3444 * as that may affect the number of constraints.
3445 * This means that we have to call isl_basic_map_gauss at the end
3446 * of the computation (in update_basic_maps) to ensure that
3447 * the basic maps are not left in an unexpected state.
3448 * For each basic map, we also compute the hash of the apparent affine hull
3449 * for use in coalesce.
3451 struct isl_map *isl_map_coalesce(struct isl_map *map)
3453 int i;
3454 unsigned n;
3455 isl_ctx *ctx;
3456 struct isl_coalesce_info *info = NULL;
3458 map = isl_map_remove_empty_parts(map);
3459 if (!map)
3460 return NULL;
3462 if (map->n <= 1)
3463 return map;
3465 ctx = isl_map_get_ctx(map);
3466 map = isl_map_sort_divs(map);
3467 map = isl_map_cow(map);
3469 if (!map)
3470 return NULL;
3472 n = map->n;
3474 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3475 if (!info)
3476 goto error;
3478 for (i = 0; i < map->n; ++i) {
3479 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3480 if (!map->p[i])
3481 goto error;
3482 info[i].bmap = isl_basic_map_copy(map->p[i]);
3483 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3484 if (!info[i].tab)
3485 goto error;
3486 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3487 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3488 goto error;
3489 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3490 info[i].bmap);
3491 if (!info[i].bmap)
3492 goto error;
3493 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3494 if (isl_tab_detect_redundant(info[i].tab) < 0)
3495 goto error;
3496 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3497 goto error;
3499 for (i = map->n - 1; i >= 0; --i)
3500 if (info[i].tab->empty)
3501 drop(&info[i]);
3503 if (coalesce(ctx, n, info) < 0)
3504 goto error;
3506 map = update_basic_maps(map, n, info);
3508 clear_coalesce_info(n, info);
3510 return map;
3511 error:
3512 clear_coalesce_info(n, info);
3513 isl_map_free(map);
3514 return NULL;
3517 /* For each pair of basic sets in the set, check if the union of the two
3518 * can be represented by a single basic set.
3519 * If so, replace the pair by the single basic set and start over.
3521 struct isl_set *isl_set_coalesce(struct isl_set *set)
3523 return set_from_map(isl_map_coalesce(set_to_map(set)));