isl_map_coalesce: detect constants among expanded divs
[isl.git] / isl_coalesce.c
blob09422713337b243d3211bae1f8582bf49c8df8ad
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 * If so, then we know that this result is exactly equal to basic map "j"
619 * since all its constraints are valid for basic map "j".
620 * By combining the valid constraints of "i" (all equalities and all
621 * inequalities except "k") and the valid constraints of "j" we therefore
622 * obtain a basic map that is equal to their union.
623 * In this case, there is no need to perform a rollback of the tableau
624 * since it is going to be destroyed in fuse().
627 * |\__ |\__
628 * | \__ | \__
629 * | \_ => | \__
630 * |_______| _ |_________\
633 * |\ |\
634 * | \ | \
635 * | \ | \
636 * | | | \
637 * | ||\ => | \
638 * | || \ | \
639 * | || | | |
640 * |__||_/ |_____/
642 static enum isl_change is_adj_ineq_extension(int i, int j,
643 struct isl_coalesce_info *info)
645 int k;
646 struct isl_tab_undo *snap;
647 unsigned n_eq = info[i].bmap->n_eq;
648 unsigned total = isl_basic_map_total_dim(info[i].bmap);
649 int r;
650 int super;
652 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
653 return isl_change_error;
655 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
656 if (k < 0)
657 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
658 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
659 return isl_change_error);
661 snap = isl_tab_snap(info[i].tab);
663 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
664 return isl_change_error;
666 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
667 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
668 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
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 if (r < 0)
672 return isl_change_error;
674 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
675 if (info[j].ineq[k] != STATUS_VALID)
676 continue;
677 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
678 return isl_change_error;
681 super = contains(&info[j], info[i].tab);
682 if (super < 0)
683 return isl_change_error;
684 if (super)
685 return fuse(i, j, info, NULL, 0, 0);
687 if (isl_tab_rollback(info[i].tab, snap) < 0)
688 return isl_change_error;
690 return isl_change_none;
694 /* Both basic maps have at least one inequality with and adjacent
695 * (but opposite) inequality in the other basic map.
696 * Check that there are no cut constraints and that there is only
697 * a single pair of adjacent inequalities.
698 * If so, we can replace the pair by a single basic map described
699 * by all but the pair of adjacent inequalities.
700 * Any additional points introduced lie strictly between the two
701 * adjacent hyperplanes and can therefore be integral.
703 * ____ _____
704 * / ||\ / \
705 * / || \ / \
706 * \ || \ => \ \
707 * \ || / \ /
708 * \___||_/ \_____/
710 * The test for a single pair of adjancent inequalities is important
711 * for avoiding the combination of two basic maps like the following
713 * /|
714 * / |
715 * /__|
716 * _____
717 * | |
718 * | |
719 * |___|
721 * If there are some cut constraints on one side, then we may
722 * still be able to fuse the two basic maps, but we need to perform
723 * some additional checks in is_adj_ineq_extension.
725 static enum isl_change check_adj_ineq(int i, int j,
726 struct isl_coalesce_info *info)
728 int count_i, count_j;
729 int cut_i, cut_j;
731 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
732 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
734 if (count_i != 1 && count_j != 1)
735 return isl_change_none;
737 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
738 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
739 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
740 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
742 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
743 return fuse(i, j, info, NULL, 0, 0);
745 if (count_i == 1 && !cut_i)
746 return is_adj_ineq_extension(i, j, info);
748 if (count_j == 1 && !cut_j)
749 return is_adj_ineq_extension(j, i, info);
751 return isl_change_none;
754 /* Given an affine transformation matrix "T", does row "row" represent
755 * anything other than a unit vector (possibly shifted by a constant)
756 * that is not involved in any of the other rows?
758 * That is, if a constraint involves the variable corresponding to
759 * the row, then could its preimage by "T" have any coefficients
760 * that are different from those in the original constraint?
762 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
764 int i, j;
765 int len = T->n_col - 1;
767 i = isl_seq_first_non_zero(T->row[row] + 1, len);
768 if (i < 0)
769 return 1;
770 if (!isl_int_is_one(T->row[row][1 + i]) &&
771 !isl_int_is_negone(T->row[row][1 + i]))
772 return 1;
774 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
775 if (j >= 0)
776 return 1;
778 for (j = 1; j < T->n_row; ++j) {
779 if (j == row)
780 continue;
781 if (!isl_int_is_zero(T->row[j][1 + i]))
782 return 1;
785 return 0;
788 /* Does inequality constraint "ineq" of "bmap" involve any of
789 * the variables marked in "affected"?
790 * "total" is the total number of variables, i.e., the number
791 * of entries in "affected".
793 static int is_affected(__isl_keep isl_basic_map *bmap, int ineq, int *affected,
794 int total)
796 int i;
798 for (i = 0; i < total; ++i) {
799 if (!affected[i])
800 continue;
801 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
802 return 1;
805 return 0;
808 /* Given the compressed version of inequality constraint "ineq"
809 * of info->bmap in "v", check if the constraint can be tightened,
810 * where the compression is based on an equality constraint valid
811 * for info->tab.
812 * If so, add the tightened version of the inequality constraint
813 * to info->tab. "v" may be modified by this function.
815 * That is, if the compressed constraint is of the form
817 * m f() + c >= 0
819 * with 0 < c < m, then it is equivalent to
821 * f() >= 0
823 * This means that c can also be subtracted from the original,
824 * uncompressed constraint without affecting the integer points
825 * in info->tab. Add this tightened constraint as an extra row
826 * to info->tab to make this information explicitly available.
828 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
829 int ineq, __isl_take isl_vec *v)
831 isl_ctx *ctx;
832 int r;
834 if (!v)
835 return NULL;
837 ctx = isl_vec_get_ctx(v);
838 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
839 if (isl_int_is_zero(ctx->normalize_gcd) ||
840 isl_int_is_one(ctx->normalize_gcd)) {
841 return v;
844 v = isl_vec_cow(v);
845 if (!v)
846 return NULL;
848 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
849 if (isl_int_is_zero(v->el[0]))
850 return v;
852 if (isl_tab_extend_cons(info->tab, 1) < 0)
853 return isl_vec_free(v);
855 isl_int_sub(info->bmap->ineq[ineq][0],
856 info->bmap->ineq[ineq][0], v->el[0]);
857 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
858 isl_int_add(info->bmap->ineq[ineq][0],
859 info->bmap->ineq[ineq][0], v->el[0]);
861 if (r < 0)
862 return isl_vec_free(v);
864 return v;
867 /* Tighten the (non-redundant) constraints on the facet represented
868 * by info->tab.
869 * In particular, on input, info->tab represents the result
870 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
871 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
872 * replacing the one at index "l" by the corresponding equality,
873 * i.e., f_k + 1 = 0, with k = relaxed[l].
875 * Compute a variable compression from the equality constraint f_k + 1 = 0
876 * and use it to tighten the other constraints of info->bmap
877 * (that is, all constraints that have not been relaxed),
878 * updating info->tab (and leaving info->bmap untouched).
879 * The compression handles essentially two cases, one where a variable
880 * is assigned a fixed value and can therefore be eliminated, and one
881 * where one variable is a shifted multiple of some other variable and
882 * can therefore be replaced by that multiple.
883 * Gaussian elimination would also work for the first case, but for
884 * the second case, the effectiveness would depend on the order
885 * of the variables.
886 * After compression, some of the constraints may have coefficients
887 * with a common divisor. If this divisor does not divide the constant
888 * term, then the constraint can be tightened.
889 * The tightening is performed on the tableau info->tab by introducing
890 * extra (temporary) constraints.
892 * Only constraints that are possibly affected by the compression are
893 * considered. In particular, if the constraint only involves variables
894 * that are directly mapped to a distinct set of other variables, then
895 * no common divisor can be introduced and no tightening can occur.
897 * It is important to only consider the non-redundant constraints
898 * since the facet constraint has been relaxed prior to the call
899 * to this function, meaning that the constraints that were redundant
900 * prior to the relaxation may no longer be redundant.
901 * These constraints will be ignored in the fused result, so
902 * the fusion detection should not exploit them.
904 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
905 int n, int *relaxed, int l)
907 unsigned total;
908 isl_ctx *ctx;
909 isl_vec *v = NULL;
910 isl_mat *T;
911 int i;
912 int k;
913 int *affected;
915 k = relaxed[l];
916 ctx = isl_basic_map_get_ctx(info->bmap);
917 total = isl_basic_map_total_dim(info->bmap);
918 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
919 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
920 T = isl_mat_variable_compression(T, NULL);
921 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
922 if (!T)
923 return isl_stat_error;
924 if (T->n_col == 0) {
925 isl_mat_free(T);
926 return isl_stat_ok;
929 affected = isl_alloc_array(ctx, int, total);
930 if (!affected)
931 goto error;
933 for (i = 0; i < total; ++i)
934 affected[i] = not_unique_unit_row(T, 1 + i);
936 for (i = 0; i < info->bmap->n_ineq; ++i) {
937 if (any(relaxed, n, i))
938 continue;
939 if (info->ineq[i] == STATUS_REDUNDANT)
940 continue;
941 if (!is_affected(info->bmap, i, affected, total))
942 continue;
943 v = isl_vec_alloc(ctx, 1 + total);
944 if (!v)
945 goto error;
946 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
947 v = isl_vec_mat_product(v, isl_mat_copy(T));
948 v = try_tightening(info, i, v);
949 isl_vec_free(v);
950 if (!v)
951 goto error;
954 isl_mat_free(T);
955 free(affected);
956 return isl_stat_ok;
957 error:
958 isl_mat_free(T);
959 free(affected);
960 return isl_stat_error;
963 /* Replace the basic maps "i" and "j" by an extension of "i"
964 * along the "n" inequality constraints in "relax" by one.
965 * The tableau info[i].tab has already been extended.
966 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
967 * by one.
968 * Each integer division that does not have exactly the same
969 * definition in "i" and "j" is marked unknown and the basic map
970 * is scheduled to be simplified in an attempt to recover
971 * the integer division definition.
972 * Place the extension in the position that is the smallest of i and j.
974 static enum isl_change extend(int i, int j, int n, int *relax,
975 struct isl_coalesce_info *info)
977 int l;
978 unsigned total;
980 info[i].bmap = isl_basic_map_cow(info[i].bmap);
981 if (!info[i].bmap)
982 return isl_change_error;
983 total = isl_basic_map_total_dim(info[i].bmap);
984 for (l = 0; l < info[i].bmap->n_div; ++l)
985 if (!isl_seq_eq(info[i].bmap->div[l],
986 info[j].bmap->div[l], 1 + 1 + total)) {
987 isl_int_set_si(info[i].bmap->div[l][0], 0);
988 info[i].simplify = 1;
990 for (l = 0; l < n; ++l)
991 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
992 info[i].bmap->ineq[relax[l]][0], 1);
993 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
994 drop(&info[j]);
995 if (j < i)
996 exchange(&info[i], &info[j]);
997 return isl_change_fuse;
1000 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1001 * that are such that they include basic map "j" if they are relaxed
1002 * by one. All the other inequalities are valid for "j".
1003 * Check if basic map "j" forms an extension of basic map "i".
1005 * In particular, relax the constraints in "relax", compute the corresponding
1006 * facets one by one and check whether each of these is included
1007 * in the other basic map.
1008 * Before testing for inclusion, the constraints on each facet
1009 * are tightened to increase the chance of an inclusion being detected.
1010 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1011 * in is_adj_ineq_extension, may further increase those chances, but this
1012 * is not currently done.)
1013 * If each facet is included, we know that relaxing the constraints extends
1014 * the basic map with exactly the other basic map (we already know that this
1015 * other basic map is included in the extension, because all other
1016 * inequality constraints are valid of "j") and we can replace the
1017 * two basic maps by this extension.
1018 * ____ _____
1019 * / || / |
1020 * / || / |
1021 * \ || => \ |
1022 * \ || \ |
1023 * \___|| \____|
1026 * \ |\
1027 * |\\ | \
1028 * | \\ | \
1029 * | | => | /
1030 * | / | /
1031 * |/ |/
1033 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1034 struct isl_coalesce_info *info)
1036 int l;
1037 int super;
1038 struct isl_tab_undo *snap, *snap2;
1039 unsigned n_eq = info[i].bmap->n_eq;
1041 for (l = 0; l < n; ++l)
1042 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1043 return isl_change_none;
1045 snap = isl_tab_snap(info[i].tab);
1046 for (l = 0; l < n; ++l)
1047 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1048 return isl_change_error;
1049 snap2 = isl_tab_snap(info[i].tab);
1050 for (l = 0; l < n; ++l) {
1051 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1052 return isl_change_error;
1053 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1054 return isl_change_error;
1055 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1056 return isl_change_error;
1057 super = contains(&info[j], info[i].tab);
1058 if (super < 0)
1059 return isl_change_error;
1060 if (super)
1061 continue;
1062 if (isl_tab_rollback(info[i].tab, snap) < 0)
1063 return isl_change_error;
1064 return isl_change_none;
1067 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1068 return isl_change_error;
1069 return extend(i, j, n, relax, info);
1072 /* Data structure that keeps track of the wrapping constraints
1073 * and of information to bound the coefficients of those constraints.
1075 * bound is set if we want to apply a bound on the coefficients
1076 * mat contains the wrapping constraints
1077 * max is the bound on the coefficients (if bound is set)
1079 struct isl_wraps {
1080 int bound;
1081 isl_mat *mat;
1082 isl_int max;
1085 /* Update wraps->max to be greater than or equal to the coefficients
1086 * in the equalities and inequalities of info->bmap that can be removed
1087 * if we end up applying wrapping.
1089 static void wraps_update_max(struct isl_wraps *wraps,
1090 struct isl_coalesce_info *info)
1092 int k;
1093 isl_int max_k;
1094 unsigned total = isl_basic_map_total_dim(info->bmap);
1096 isl_int_init(max_k);
1098 for (k = 0; k < info->bmap->n_eq; ++k) {
1099 if (info->eq[2 * k] == STATUS_VALID &&
1100 info->eq[2 * k + 1] == STATUS_VALID)
1101 continue;
1102 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1103 if (isl_int_abs_gt(max_k, wraps->max))
1104 isl_int_set(wraps->max, max_k);
1107 for (k = 0; k < info->bmap->n_ineq; ++k) {
1108 if (info->ineq[k] == STATUS_VALID ||
1109 info->ineq[k] == STATUS_REDUNDANT)
1110 continue;
1111 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1112 if (isl_int_abs_gt(max_k, wraps->max))
1113 isl_int_set(wraps->max, max_k);
1116 isl_int_clear(max_k);
1119 /* Initialize the isl_wraps data structure.
1120 * If we want to bound the coefficients of the wrapping constraints,
1121 * we set wraps->max to the largest coefficient
1122 * in the equalities and inequalities that can be removed if we end up
1123 * applying wrapping.
1125 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1126 struct isl_coalesce_info *info, int i, int j)
1128 isl_ctx *ctx;
1130 wraps->bound = 0;
1131 wraps->mat = mat;
1132 if (!mat)
1133 return;
1134 ctx = isl_mat_get_ctx(mat);
1135 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1136 if (!wraps->bound)
1137 return;
1138 isl_int_init(wraps->max);
1139 isl_int_set_si(wraps->max, 0);
1140 wraps_update_max(wraps, &info[i]);
1141 wraps_update_max(wraps, &info[j]);
1144 /* Free the contents of the isl_wraps data structure.
1146 static void wraps_free(struct isl_wraps *wraps)
1148 isl_mat_free(wraps->mat);
1149 if (wraps->bound)
1150 isl_int_clear(wraps->max);
1153 /* Is the wrapping constraint in row "row" allowed?
1155 * If wraps->bound is set, we check that none of the coefficients
1156 * is greater than wraps->max.
1158 static int allow_wrap(struct isl_wraps *wraps, int row)
1160 int i;
1162 if (!wraps->bound)
1163 return 1;
1165 for (i = 1; i < wraps->mat->n_col; ++i)
1166 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1167 return 0;
1169 return 1;
1172 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1173 * to include "set" and add the result in position "w" of "wraps".
1174 * "len" is the total number of coefficients in "bound" and "ineq".
1175 * Return 1 on success, 0 on failure and -1 on error.
1176 * Wrapping can fail if the result of wrapping is equal to "bound"
1177 * or if we want to bound the sizes of the coefficients and
1178 * the wrapped constraint does not satisfy this bound.
1180 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1181 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1183 isl_seq_cpy(wraps->mat->row[w], bound, len);
1184 if (negate) {
1185 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1186 ineq = wraps->mat->row[w + 1];
1188 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1189 return -1;
1190 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1191 return 0;
1192 if (!allow_wrap(wraps, w))
1193 return 0;
1194 return 1;
1197 /* For each constraint in info->bmap that is not redundant (as determined
1198 * by info->tab) and that is not a valid constraint for the other basic map,
1199 * wrap the constraint around "bound" such that it includes the whole
1200 * set "set" and append the resulting constraint to "wraps".
1201 * Note that the constraints that are valid for the other basic map
1202 * will be added to the combined basic map by default, so there is
1203 * no need to wrap them.
1204 * The caller wrap_in_facets even relies on this function not wrapping
1205 * any constraints that are already valid.
1206 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1207 * wraps->n_row is the number of actual wrapped constraints that have
1208 * been added.
1209 * If any of the wrapping problems results in a constraint that is
1210 * identical to "bound", then this means that "set" is unbounded in such
1211 * way that no wrapping is possible. If this happens then wraps->n_row
1212 * is reset to zero.
1213 * Similarly, if we want to bound the coefficients of the wrapping
1214 * constraints and a newly added wrapping constraint does not
1215 * satisfy the bound, then wraps->n_row is also reset to zero.
1217 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1218 isl_int *bound, __isl_keep isl_set *set)
1220 int l, m;
1221 int w;
1222 int added;
1223 isl_basic_map *bmap = info->bmap;
1224 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1226 w = wraps->mat->n_row;
1228 for (l = 0; l < bmap->n_ineq; ++l) {
1229 if (info->ineq[l] == STATUS_VALID ||
1230 info->ineq[l] == STATUS_REDUNDANT)
1231 continue;
1232 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1233 continue;
1234 if (isl_seq_eq(bound, bmap->ineq[l], len))
1235 continue;
1236 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1237 continue;
1239 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1240 if (added < 0)
1241 return -1;
1242 if (!added)
1243 goto unbounded;
1244 ++w;
1246 for (l = 0; l < bmap->n_eq; ++l) {
1247 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1248 continue;
1249 if (isl_seq_eq(bound, bmap->eq[l], len))
1250 continue;
1252 for (m = 0; m < 2; ++m) {
1253 if (info->eq[2 * l + m] == STATUS_VALID)
1254 continue;
1255 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1256 set, !m);
1257 if (added < 0)
1258 return -1;
1259 if (!added)
1260 goto unbounded;
1261 ++w;
1265 wraps->mat->n_row = w;
1266 return 0;
1267 unbounded:
1268 wraps->mat->n_row = 0;
1269 return 0;
1272 /* Check if the constraints in "wraps" from "first" until the last
1273 * are all valid for the basic set represented by "tab".
1274 * If not, wraps->n_row is set to zero.
1276 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1277 struct isl_tab *tab)
1279 int i;
1281 for (i = first; i < wraps->n_row; ++i) {
1282 enum isl_ineq_type type;
1283 type = isl_tab_ineq_type(tab, wraps->row[i]);
1284 if (type == isl_ineq_error)
1285 return -1;
1286 if (type == isl_ineq_redundant)
1287 continue;
1288 wraps->n_row = 0;
1289 return 0;
1292 return 0;
1295 /* Return a set that corresponds to the non-redundant constraints
1296 * (as recorded in tab) of bmap.
1298 * It's important to remove the redundant constraints as some
1299 * of the other constraints may have been modified after the
1300 * constraints were marked redundant.
1301 * In particular, a constraint may have been relaxed.
1302 * Redundant constraints are ignored when a constraint is relaxed
1303 * and should therefore continue to be ignored ever after.
1304 * Otherwise, the relaxation might be thwarted by some of
1305 * these constraints.
1307 * Update the underlying set to ensure that the dimension doesn't change.
1308 * Otherwise the integer divisions could get dropped if the tab
1309 * turns out to be empty.
1311 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1312 struct isl_tab *tab)
1314 isl_basic_set *bset;
1316 bmap = isl_basic_map_copy(bmap);
1317 bset = isl_basic_map_underlying_set(bmap);
1318 bset = isl_basic_set_cow(bset);
1319 bset = isl_basic_set_update_from_tab(bset, tab);
1320 return isl_set_from_basic_set(bset);
1323 /* Wrap the constraints of info->bmap that bound the facet defined
1324 * by inequality "k" around (the opposite of) this inequality to
1325 * include "set". "bound" may be used to store the negated inequality.
1326 * Since the wrapped constraints are not guaranteed to contain the whole
1327 * of info->bmap, we check them in check_wraps.
1328 * If any of the wrapped constraints turn out to be invalid, then
1329 * check_wraps will reset wrap->n_row to zero.
1331 static int add_wraps_around_facet(struct isl_wraps *wraps,
1332 struct isl_coalesce_info *info, int k, isl_int *bound,
1333 __isl_keep isl_set *set)
1335 struct isl_tab_undo *snap;
1336 int n;
1337 unsigned total = isl_basic_map_total_dim(info->bmap);
1339 snap = isl_tab_snap(info->tab);
1341 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1342 return -1;
1343 if (isl_tab_detect_redundant(info->tab) < 0)
1344 return -1;
1346 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1348 n = wraps->mat->n_row;
1349 if (add_wraps(wraps, info, bound, set) < 0)
1350 return -1;
1352 if (isl_tab_rollback(info->tab, snap) < 0)
1353 return -1;
1354 if (check_wraps(wraps->mat, n, info->tab) < 0)
1355 return -1;
1357 return 0;
1360 /* Given a basic set i with a constraint k that is adjacent to
1361 * basic set j, check if we can wrap
1362 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1363 * (always) around their ridges to include the other set.
1364 * If so, replace the pair of basic sets by their union.
1366 * All constraints of i (except k) are assumed to be valid or
1367 * cut constraints for j.
1368 * Wrapping the cut constraints to include basic map j may result
1369 * in constraints that are no longer valid of basic map i
1370 * we have to check that the resulting wrapping constraints are valid for i.
1371 * If "wrap_facet" is not set, then all constraints of i (except k)
1372 * are assumed to be valid for j.
1373 * ____ _____
1374 * / | / \
1375 * / || / |
1376 * \ || => \ |
1377 * \ || \ |
1378 * \___|| \____|
1381 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1382 struct isl_coalesce_info *info, int wrap_facet)
1384 enum isl_change change = isl_change_none;
1385 struct isl_wraps wraps;
1386 isl_ctx *ctx;
1387 isl_mat *mat;
1388 struct isl_set *set_i = NULL;
1389 struct isl_set *set_j = NULL;
1390 struct isl_vec *bound = NULL;
1391 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1393 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1394 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1395 ctx = isl_basic_map_get_ctx(info[i].bmap);
1396 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1397 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1398 1 + total);
1399 wraps_init(&wraps, mat, info, i, j);
1400 bound = isl_vec_alloc(ctx, 1 + total);
1401 if (!set_i || !set_j || !wraps.mat || !bound)
1402 goto error;
1404 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1405 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1407 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1408 wraps.mat->n_row = 1;
1410 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1411 goto error;
1412 if (!wraps.mat->n_row)
1413 goto unbounded;
1415 if (wrap_facet) {
1416 if (add_wraps_around_facet(&wraps, &info[i], k,
1417 bound->el, set_j) < 0)
1418 goto error;
1419 if (!wraps.mat->n_row)
1420 goto unbounded;
1423 change = fuse(i, j, info, wraps.mat, 0, 0);
1425 unbounded:
1426 wraps_free(&wraps);
1428 isl_set_free(set_i);
1429 isl_set_free(set_j);
1431 isl_vec_free(bound);
1433 return change;
1434 error:
1435 wraps_free(&wraps);
1436 isl_vec_free(bound);
1437 isl_set_free(set_i);
1438 isl_set_free(set_j);
1439 return isl_change_error;
1442 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1443 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1444 * add wrapping constraints to wrap.mat for all constraints
1445 * of basic map j that bound the part of basic map j that sticks out
1446 * of the cut constraint.
1447 * "set_i" is the underlying set of basic map i.
1448 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1450 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1451 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1452 * (with respect to the integer points), so we add t(x) >= 0 instead.
1453 * Otherwise, we wrap the constraints of basic map j that are not
1454 * redundant in this intersection and that are not already valid
1455 * for basic map i over basic map i.
1456 * Note that it is sufficient to wrap the constraints to include
1457 * basic map i, because we will only wrap the constraints that do
1458 * not include basic map i already. The wrapped constraint will
1459 * therefore be more relaxed compared to the original constraint.
1460 * Since the original constraint is valid for basic map j, so is
1461 * the wrapped constraint.
1463 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1464 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1465 struct isl_tab_undo *snap)
1467 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1468 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1469 return isl_stat_error;
1470 if (isl_tab_detect_redundant(info_j->tab) < 0)
1471 return isl_stat_error;
1473 if (info_j->tab->empty)
1474 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1475 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1476 return isl_stat_error;
1478 if (isl_tab_rollback(info_j->tab, snap) < 0)
1479 return isl_stat_error;
1481 return isl_stat_ok;
1484 /* Given a pair of basic maps i and j such that j sticks out
1485 * of i at n cut constraints, each time by at most one,
1486 * try to compute wrapping constraints and replace the two
1487 * basic maps by a single basic map.
1488 * The other constraints of i are assumed to be valid for j.
1489 * "set_i" is the underlying set of basic map i.
1490 * "wraps" has been initialized to be of the right size.
1492 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1493 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1494 * of basic map j that bound the part of basic map j that sticks out
1495 * of the cut constraint.
1497 * If any wrapping fails, i.e., if we cannot wrap to touch
1498 * the union, then we give up.
1499 * Otherwise, the pair of basic maps is replaced by their union.
1501 static enum isl_change try_wrap_in_facets(int i, int j,
1502 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1503 __isl_keep isl_set *set_i)
1505 int k, l, w;
1506 unsigned total;
1507 struct isl_tab_undo *snap;
1509 total = isl_basic_map_total_dim(info[i].bmap);
1511 snap = isl_tab_snap(info[j].tab);
1513 wraps->mat->n_row = 0;
1515 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1516 for (l = 0; l < 2; ++l) {
1517 if (info[i].eq[2 * k + l] != STATUS_CUT)
1518 continue;
1519 w = wraps->mat->n_row++;
1520 if (l == 0)
1521 isl_seq_neg(wraps->mat->row[w],
1522 info[i].bmap->eq[k], 1 + total);
1523 else
1524 isl_seq_cpy(wraps->mat->row[w],
1525 info[i].bmap->eq[k], 1 + total);
1526 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1527 return isl_change_error;
1529 if (!wraps->mat->n_row)
1530 return isl_change_none;
1534 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1535 if (info[i].ineq[k] != STATUS_CUT)
1536 continue;
1537 w = wraps->mat->n_row++;
1538 isl_seq_cpy(wraps->mat->row[w],
1539 info[i].bmap->ineq[k], 1 + total);
1540 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1541 return isl_change_error;
1543 if (!wraps->mat->n_row)
1544 return isl_change_none;
1547 return fuse(i, j, info, wraps->mat, 0, 1);
1550 /* Given a pair of basic maps i and j such that j sticks out
1551 * of i at n cut constraints, each time by at most one,
1552 * try to compute wrapping constraints and replace the two
1553 * basic maps by a single basic map.
1554 * The other constraints of i are assumed to be valid for j.
1556 * The core computation is performed by try_wrap_in_facets.
1557 * This function simply extracts an underlying set representation
1558 * of basic map i and initializes the data structure for keeping
1559 * track of wrapping constraints.
1561 static enum isl_change wrap_in_facets(int i, int j, int n,
1562 struct isl_coalesce_info *info)
1564 enum isl_change change = isl_change_none;
1565 struct isl_wraps wraps;
1566 isl_ctx *ctx;
1567 isl_mat *mat;
1568 isl_set *set_i = NULL;
1569 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1570 int max_wrap;
1572 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1573 return isl_change_error;
1575 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1576 max_wrap *= n;
1578 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1579 ctx = isl_basic_map_get_ctx(info[i].bmap);
1580 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1581 wraps_init(&wraps, mat, info, i, j);
1582 if (!set_i || !wraps.mat)
1583 goto error;
1585 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1587 wraps_free(&wraps);
1588 isl_set_free(set_i);
1590 return change;
1591 error:
1592 wraps_free(&wraps);
1593 isl_set_free(set_i);
1594 return isl_change_error;
1597 /* Return the effect of inequality "ineq" on the tableau "tab",
1598 * after relaxing the constant term of "ineq" by one.
1600 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1602 enum isl_ineq_type type;
1604 isl_int_add_ui(ineq[0], ineq[0], 1);
1605 type = isl_tab_ineq_type(tab, ineq);
1606 isl_int_sub_ui(ineq[0], ineq[0], 1);
1608 return type;
1611 /* Given two basic sets i and j,
1612 * check if relaxing all the cut constraints of i by one turns
1613 * them into valid constraint for j and check if we can wrap in
1614 * the bits that are sticking out.
1615 * If so, replace the pair by their union.
1617 * We first check if all relaxed cut inequalities of i are valid for j
1618 * and then try to wrap in the intersections of the relaxed cut inequalities
1619 * with j.
1621 * During this wrapping, we consider the points of j that lie at a distance
1622 * of exactly 1 from i. In particular, we ignore the points that lie in
1623 * between this lower-dimensional space and the basic map i.
1624 * We can therefore only apply this to integer maps.
1625 * ____ _____
1626 * / ___|_ / \
1627 * / | | / |
1628 * \ | | => \ |
1629 * \|____| \ |
1630 * \___| \____/
1632 * _____ ______
1633 * | ____|_ | \
1634 * | | | | |
1635 * | | | => | |
1636 * |_| | | |
1637 * |_____| \______|
1639 * _______
1640 * | |
1641 * | |\ |
1642 * | | \ |
1643 * | | \ |
1644 * | | \|
1645 * | | \
1646 * | |_____\
1647 * | |
1648 * |_______|
1650 * Wrapping can fail if the result of wrapping one of the facets
1651 * around its edges does not produce any new facet constraint.
1652 * In particular, this happens when we try to wrap in unbounded sets.
1654 * _______________________________________________________________________
1656 * | ___
1657 * | | |
1658 * |_| |_________________________________________________________________
1659 * |___|
1661 * The following is not an acceptable result of coalescing the above two
1662 * sets as it includes extra integer points.
1663 * _______________________________________________________________________
1665 * |
1666 * |
1668 * \______________________________________________________________________
1670 static enum isl_change can_wrap_in_set(int i, int j,
1671 struct isl_coalesce_info *info)
1673 int k, l;
1674 int n;
1675 unsigned total;
1677 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1678 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1679 return isl_change_none;
1681 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1682 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1683 if (n == 0)
1684 return isl_change_none;
1686 total = isl_basic_map_total_dim(info[i].bmap);
1687 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1688 for (l = 0; l < 2; ++l) {
1689 enum isl_ineq_type type;
1691 if (info[i].eq[2 * k + l] != STATUS_CUT)
1692 continue;
1694 if (l == 0)
1695 isl_seq_neg(info[i].bmap->eq[k],
1696 info[i].bmap->eq[k], 1 + total);
1697 type = type_of_relaxed(info[j].tab,
1698 info[i].bmap->eq[k]);
1699 if (l == 0)
1700 isl_seq_neg(info[i].bmap->eq[k],
1701 info[i].bmap->eq[k], 1 + total);
1702 if (type == isl_ineq_error)
1703 return isl_change_error;
1704 if (type != isl_ineq_redundant)
1705 return isl_change_none;
1709 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1710 enum isl_ineq_type type;
1712 if (info[i].ineq[k] != STATUS_CUT)
1713 continue;
1715 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1716 if (type == isl_ineq_error)
1717 return isl_change_error;
1718 if (type != isl_ineq_redundant)
1719 return isl_change_none;
1722 return wrap_in_facets(i, j, n, info);
1725 /* Check if either i or j has only cut constraints that can
1726 * be used to wrap in (a facet of) the other basic set.
1727 * if so, replace the pair by their union.
1729 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1731 enum isl_change change = isl_change_none;
1733 change = can_wrap_in_set(i, j, info);
1734 if (change != isl_change_none)
1735 return change;
1737 change = can_wrap_in_set(j, i, info);
1738 return change;
1741 /* Check if all inequality constraints of "i" that cut "j" cease
1742 * to be cut constraints if they are relaxed by one.
1743 * If so, collect the cut constraints in "list".
1744 * The caller is responsible for allocating "list".
1746 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1747 int *list)
1749 int l, n;
1751 n = 0;
1752 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1753 enum isl_ineq_type type;
1755 if (info[i].ineq[l] != STATUS_CUT)
1756 continue;
1757 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1758 if (type == isl_ineq_error)
1759 return isl_bool_error;
1760 if (type != isl_ineq_redundant)
1761 return isl_bool_false;
1762 list[n++] = l;
1765 return isl_bool_true;
1768 /* Given two basic maps such that "j" has at least one equality constraint
1769 * that is adjacent to an inequality constraint of "i" and such that "i" has
1770 * exactly one inequality constraint that is adjacent to an equality
1771 * constraint of "j", check whether "i" can be extended to include "j" or
1772 * whether "j" can be wrapped into "i".
1773 * All remaining constraints of "i" and "j" are assumed to be valid
1774 * or cut constraints of the other basic map.
1775 * However, none of the equality constraints of "i" are cut constraints.
1777 * If "i" has any "cut" inequality constraints, then check if relaxing
1778 * each of them by one is sufficient for them to become valid.
1779 * If so, check if the inequality constraint adjacent to an equality
1780 * constraint of "j" along with all these cut constraints
1781 * can be relaxed by one to contain exactly "j".
1782 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1784 static enum isl_change check_single_adj_eq(int i, int j,
1785 struct isl_coalesce_info *info)
1787 enum isl_change change = isl_change_none;
1788 int k;
1789 int n_cut;
1790 int *relax;
1791 isl_ctx *ctx;
1792 isl_bool try_relax;
1794 n_cut = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1796 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ);
1798 if (n_cut > 0) {
1799 ctx = isl_basic_map_get_ctx(info[i].bmap);
1800 relax = isl_calloc_array(ctx, int, 1 + n_cut);
1801 if (!relax)
1802 return isl_change_error;
1803 relax[0] = k;
1804 try_relax = all_cut_by_one(i, j, info, relax + 1);
1805 if (try_relax < 0)
1806 change = isl_change_error;
1807 } else {
1808 try_relax = isl_bool_true;
1809 relax = &k;
1811 if (try_relax && change == isl_change_none)
1812 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
1813 if (n_cut > 0)
1814 free(relax);
1815 if (change != isl_change_none)
1816 return change;
1818 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
1820 return change;
1823 /* At least one of the basic maps has an equality that is adjacent
1824 * to inequality. Make sure that only one of the basic maps has
1825 * such an equality and that the other basic map has exactly one
1826 * inequality adjacent to an equality.
1827 * If the other basic map does not have such an inequality, then
1828 * check if all its constraints are either valid or cut constraints
1829 * and, if so, try wrapping in the first map into the second.
1830 * Otherwise, try to extend one basic map with the other or
1831 * wrap one basic map in the other.
1833 static enum isl_change check_adj_eq(int i, int j,
1834 struct isl_coalesce_info *info)
1836 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1837 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1838 /* ADJ EQ TOO MANY */
1839 return isl_change_none;
1841 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1842 return check_adj_eq(j, i, info);
1844 /* j has an equality adjacent to an inequality in i */
1846 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1847 if (all_valid_or_cut(&info[i]))
1848 return can_wrap_in_set(i, j, info);
1849 return isl_change_none;
1851 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1852 return isl_change_none;
1853 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1854 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1855 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1856 /* ADJ EQ TOO MANY */
1857 return isl_change_none;
1859 return check_single_adj_eq(i, j, info);
1862 /* The two basic maps lie on adjacent hyperplanes. In particular,
1863 * basic map "i" has an equality that lies parallel to basic map "j".
1864 * Check if we can wrap the facets around the parallel hyperplanes
1865 * to include the other set.
1867 * We perform basically the same operations as can_wrap_in_facet,
1868 * except that we don't need to select a facet of one of the sets.
1870 * \\ \\
1871 * \\ => \\
1872 * \ \|
1874 * If there is more than one equality of "i" adjacent to an equality of "j",
1875 * then the result will satisfy one or more equalities that are a linear
1876 * combination of these equalities. These will be encoded as pairs
1877 * of inequalities in the wrapping constraints and need to be made
1878 * explicit.
1880 static enum isl_change check_eq_adj_eq(int i, int j,
1881 struct isl_coalesce_info *info)
1883 int k;
1884 enum isl_change change = isl_change_none;
1885 int detect_equalities = 0;
1886 struct isl_wraps wraps;
1887 isl_ctx *ctx;
1888 isl_mat *mat;
1889 struct isl_set *set_i = NULL;
1890 struct isl_set *set_j = NULL;
1891 struct isl_vec *bound = NULL;
1892 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1894 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1895 detect_equalities = 1;
1897 k = find(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ);
1899 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1900 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1901 ctx = isl_basic_map_get_ctx(info[i].bmap);
1902 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1903 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1904 1 + total);
1905 wraps_init(&wraps, mat, info, i, j);
1906 bound = isl_vec_alloc(ctx, 1 + total);
1907 if (!set_i || !set_j || !wraps.mat || !bound)
1908 goto error;
1910 if (k % 2 == 0)
1911 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1912 else
1913 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1914 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1916 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1917 wraps.mat->n_row = 1;
1919 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1920 goto error;
1921 if (!wraps.mat->n_row)
1922 goto unbounded;
1924 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1925 isl_seq_neg(bound->el, bound->el, 1 + total);
1927 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1928 wraps.mat->n_row++;
1930 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1931 goto error;
1932 if (!wraps.mat->n_row)
1933 goto unbounded;
1935 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1937 if (0) {
1938 error: change = isl_change_error;
1940 unbounded:
1942 wraps_free(&wraps);
1943 isl_set_free(set_i);
1944 isl_set_free(set_j);
1945 isl_vec_free(bound);
1947 return change;
1950 /* Initialize the "eq" and "ineq" fields of "info".
1952 static void init_status(struct isl_coalesce_info *info)
1954 info->eq = info->ineq = NULL;
1957 /* Set info->eq to the positions of the equalities of info->bmap
1958 * with respect to the basic map represented by "tab".
1959 * If info->eq has already been computed, then do not compute it again.
1961 static void set_eq_status_in(struct isl_coalesce_info *info,
1962 struct isl_tab *tab)
1964 if (info->eq)
1965 return;
1966 info->eq = eq_status_in(info->bmap, tab);
1969 /* Set info->ineq to the positions of the inequalities of info->bmap
1970 * with respect to the basic map represented by "tab".
1971 * If info->ineq has already been computed, then do not compute it again.
1973 static void set_ineq_status_in(struct isl_coalesce_info *info,
1974 struct isl_tab *tab)
1976 if (info->ineq)
1977 return;
1978 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1981 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1982 * This function assumes that init_status has been called on "info" first,
1983 * after which the "eq" and "ineq" fields may or may not have been
1984 * assigned a newly allocated array.
1986 static void clear_status(struct isl_coalesce_info *info)
1988 free(info->eq);
1989 free(info->ineq);
1992 /* Check if the union of the given pair of basic maps
1993 * can be represented by a single basic map.
1994 * If so, replace the pair by the single basic map and return
1995 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1996 * Otherwise, return isl_change_none.
1997 * The two basic maps are assumed to live in the same local space.
1998 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1999 * to have been initialized by the caller, either to NULL or
2000 * to valid information.
2002 * We first check the effect of each constraint of one basic map
2003 * on the other basic map.
2004 * The constraint may be
2005 * redundant the constraint is redundant in its own
2006 * basic map and should be ignore and removed
2007 * in the end
2008 * valid all (integer) points of the other basic map
2009 * satisfy the constraint
2010 * separate no (integer) point of the other basic map
2011 * satisfies the constraint
2012 * cut some but not all points of the other basic map
2013 * satisfy the constraint
2014 * adj_eq the given constraint is adjacent (on the outside)
2015 * to an equality of the other basic map
2016 * adj_ineq the given constraint is adjacent (on the outside)
2017 * to an inequality of the other basic map
2019 * We consider seven cases in which we can replace the pair by a single
2020 * basic map. We ignore all "redundant" constraints.
2022 * 1. all constraints of one basic map are valid
2023 * => the other basic map is a subset and can be removed
2025 * 2. all constraints of both basic maps are either "valid" or "cut"
2026 * and the facets corresponding to the "cut" constraints
2027 * of one of the basic maps lies entirely inside the other basic map
2028 * => the pair can be replaced by a basic map consisting
2029 * of the valid constraints in both basic maps
2031 * 3. there is a single pair of adjacent inequalities
2032 * (all other constraints are "valid")
2033 * => the pair can be replaced by a basic map consisting
2034 * of the valid constraints in both basic maps
2036 * 4. one basic map has a single adjacent inequality, while the other
2037 * constraints are "valid". The other basic map has some
2038 * "cut" constraints, but replacing the adjacent inequality by
2039 * its opposite and adding the valid constraints of the other
2040 * basic map results in a subset of the other basic map
2041 * => the pair can be replaced by a basic map consisting
2042 * of the valid constraints in both basic maps
2044 * 5. there is a single adjacent pair of an inequality and an equality,
2045 * the other constraints of the basic map containing the inequality are
2046 * "valid". Moreover, if the inequality the basic map is relaxed
2047 * and then turned into an equality, then resulting facet lies
2048 * entirely inside the other basic map
2049 * => the pair can be replaced by the basic map containing
2050 * the inequality, with the inequality relaxed.
2052 * 6. there is a single adjacent pair of an inequality and an equality,
2053 * the other constraints of the basic map containing the inequality are
2054 * "valid". Moreover, the facets corresponding to both
2055 * the inequality and the equality can be wrapped around their
2056 * ridges to include the other basic map
2057 * => the pair can be replaced by a basic map consisting
2058 * of the valid constraints in both basic maps together
2059 * with all wrapping constraints
2061 * 7. one of the basic maps extends beyond the other by at most one.
2062 * Moreover, the facets corresponding to the cut constraints and
2063 * the pieces of the other basic map at offset one from these cut
2064 * constraints can be wrapped around their ridges to include
2065 * the union of the two basic maps
2066 * => the pair can be replaced by a basic map consisting
2067 * of the valid constraints in both basic maps together
2068 * with all wrapping constraints
2070 * 8. the two basic maps live in adjacent hyperplanes. In principle
2071 * such sets can always be combined through wrapping, but we impose
2072 * that there is only one such pair, to avoid overeager coalescing.
2074 * Throughout the computation, we maintain a collection of tableaus
2075 * corresponding to the basic maps. When the basic maps are dropped
2076 * or combined, the tableaus are modified accordingly.
2078 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2079 struct isl_coalesce_info *info)
2081 enum isl_change change = isl_change_none;
2083 set_eq_status_in(&info[i], info[j].tab);
2084 if (info[i].bmap->n_eq && !info[i].eq)
2085 goto error;
2086 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
2087 goto error;
2088 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
2089 goto done;
2091 set_eq_status_in(&info[j], info[i].tab);
2092 if (info[j].bmap->n_eq && !info[j].eq)
2093 goto error;
2094 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
2095 goto error;
2096 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
2097 goto done;
2099 set_ineq_status_in(&info[i], info[j].tab);
2100 if (info[i].bmap->n_ineq && !info[i].ineq)
2101 goto error;
2102 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
2103 goto error;
2104 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
2105 goto done;
2107 set_ineq_status_in(&info[j], info[i].tab);
2108 if (info[j].bmap->n_ineq && !info[j].ineq)
2109 goto error;
2110 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2111 goto error;
2112 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2113 goto done;
2115 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2116 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2117 drop(&info[j]);
2118 change = isl_change_drop_second;
2119 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2120 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2121 drop(&info[i]);
2122 change = isl_change_drop_first;
2123 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2124 change = check_eq_adj_eq(i, j, info);
2125 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2126 change = check_eq_adj_eq(j, i, info);
2127 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2128 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2129 change = check_adj_eq(i, j, info);
2130 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2131 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2132 /* Can't happen */
2133 /* BAD ADJ INEQ */
2134 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2135 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2136 change = check_adj_ineq(i, j, info);
2137 } else {
2138 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2139 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2140 change = check_facets(i, j, info);
2141 if (change == isl_change_none)
2142 change = check_wrap(i, j, info);
2145 done:
2146 clear_status(&info[i]);
2147 clear_status(&info[j]);
2148 return change;
2149 error:
2150 clear_status(&info[i]);
2151 clear_status(&info[j]);
2152 return isl_change_error;
2155 /* Check if the union of the given pair of basic maps
2156 * can be represented by a single basic map.
2157 * If so, replace the pair by the single basic map and return
2158 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2159 * Otherwise, return isl_change_none.
2160 * The two basic maps are assumed to live in the same local space.
2162 static enum isl_change coalesce_local_pair(int i, int j,
2163 struct isl_coalesce_info *info)
2165 init_status(&info[i]);
2166 init_status(&info[j]);
2167 return coalesce_local_pair_reuse(i, j, info);
2170 /* Shift the integer division at position "div" of the basic map
2171 * represented by "info" by "shift".
2173 * That is, if the integer division has the form
2175 * floor(f(x)/d)
2177 * then replace it by
2179 * floor((f(x) + shift * d)/d) - shift
2181 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
2183 unsigned total;
2185 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2186 if (!info->bmap)
2187 return -1;
2189 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2190 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2191 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2192 return -1;
2194 return 0;
2197 /* Check if some of the divs in the basic map represented by "info1"
2198 * are shifts of the corresponding divs in the basic map represented
2199 * by "info2". If so, align them with those of "info2".
2200 * Only do this if "info1" and "info2" have the same number
2201 * of integer divisions.
2203 * An integer division is considered to be a shift of another integer
2204 * division if one is equal to the other plus a constant.
2206 * In particular, for each pair of integer divisions, if both are known,
2207 * have identical coefficients (apart from the constant term) and
2208 * if the difference between the constant terms (taking into account
2209 * the denominator) is an integer, then move the difference outside.
2210 * That is, if one integer division is of the form
2212 * floor((f(x) + c_1)/d)
2214 * while the other is of the form
2216 * floor((f(x) + c_2)/d)
2218 * and n = (c_2 - c_1)/d is an integer, then replace the first
2219 * integer division by
2221 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2223 static int harmonize_divs(struct isl_coalesce_info *info1,
2224 struct isl_coalesce_info *info2)
2226 int i;
2227 int total;
2229 if (!info1->bmap || !info2->bmap)
2230 return -1;
2232 if (info1->bmap->n_div != info2->bmap->n_div)
2233 return 0;
2234 if (info1->bmap->n_div == 0)
2235 return 0;
2237 total = isl_basic_map_total_dim(info1->bmap);
2238 for (i = 0; i < info1->bmap->n_div; ++i) {
2239 isl_int d;
2240 int r = 0;
2242 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
2243 isl_int_is_zero(info2->bmap->div[i][0]))
2244 continue;
2245 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2246 continue;
2247 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
2248 continue;
2249 if (!isl_seq_eq(info1->bmap->div[i] + 2,
2250 info2->bmap->div[i] + 2, total))
2251 continue;
2252 isl_int_init(d);
2253 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
2254 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
2255 isl_int_divexact(d, d, info1->bmap->div[i][0]);
2256 r = shift_div(info1, i, d);
2258 isl_int_clear(d);
2259 if (r < 0)
2260 return -1;
2263 return 0;
2266 /* Do the two basic maps live in the same local space, i.e.,
2267 * do they have the same (known) divs?
2268 * If either basic map has any unknown divs, then we can only assume
2269 * that they do not live in the same local space.
2271 static int same_divs(__isl_keep isl_basic_map *bmap1,
2272 __isl_keep isl_basic_map *bmap2)
2274 int i;
2275 int known;
2276 int total;
2278 if (!bmap1 || !bmap2)
2279 return -1;
2280 if (bmap1->n_div != bmap2->n_div)
2281 return 0;
2283 if (bmap1->n_div == 0)
2284 return 1;
2286 known = isl_basic_map_divs_known(bmap1);
2287 if (known < 0 || !known)
2288 return known;
2289 known = isl_basic_map_divs_known(bmap2);
2290 if (known < 0 || !known)
2291 return known;
2293 total = isl_basic_map_total_dim(bmap1);
2294 for (i = 0; i < bmap1->n_div; ++i)
2295 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2296 return 0;
2298 return 1;
2301 /* Assuming that "tab" contains the equality constraints and
2302 * the initial inequality constraints of "bmap", copy the remaining
2303 * inequality constraints of "bmap" to "Tab".
2305 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2307 int i, n_ineq;
2309 if (!bmap)
2310 return isl_stat_error;
2312 n_ineq = tab->n_con - tab->n_eq;
2313 for (i = n_ineq; i < bmap->n_ineq; ++i)
2314 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2315 return isl_stat_error;
2317 return isl_stat_ok;
2320 /* Description of an integer division that is added
2321 * during an expansion.
2322 * "pos" is the position of the corresponding variable.
2323 * "cst" indicates whether this integer division has a fixed value.
2324 * "val" contains the fixed value, if the value is fixed.
2326 struct isl_expanded {
2327 int pos;
2328 isl_bool cst;
2329 isl_int val;
2332 /* For each of the "n" integer division variables "expanded",
2333 * if the variable has a fixed value, then add two inequality
2334 * constraints expressing the fixed value.
2335 * Otherwise, add the corresponding div constraints.
2336 * The caller is responsible for removing the div constraints
2337 * that it added for all these "n" integer divisions.
2339 * The div constraints and the pair of inequality constraints
2340 * forcing the fixed value cannot both be added for a given variable
2341 * as the combination may render some of the original constraints redundant.
2342 * These would then be ignored during the coalescing detection,
2343 * while they could remain in the fused result.
2345 * The two added inequality constraints are
2347 * -a + v >= 0
2348 * a - v >= 0
2350 * with "a" the variable and "v" its fixed value.
2351 * The facet corresponding to one of these two constraints is selected
2352 * in the tableau to ensure that the pair of inequality constraints
2353 * is treated as an equality constraint.
2355 * The information in info->ineq is thrown away because it was
2356 * computed in terms of div constraints, while some of those
2357 * have now been replaced by these pairs of inequality constraints.
2359 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2360 int n, struct isl_expanded *expanded)
2362 unsigned o_div;
2363 int i;
2364 isl_vec *ineq;
2366 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2367 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2368 if (!ineq)
2369 return isl_stat_error;
2370 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2372 for (i = 0; i < n; ++i) {
2373 if (!expanded[i].cst) {
2374 info->bmap = isl_basic_map_extend_constraints(
2375 info->bmap, 0, 2);
2376 if (isl_basic_map_add_div_constraints(info->bmap,
2377 expanded[i].pos - o_div) < 0)
2378 break;
2379 } else {
2380 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2381 isl_int_set(ineq->el[0], expanded[i].val);
2382 info->bmap = isl_basic_map_add_ineq(info->bmap,
2383 ineq->el);
2384 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2385 isl_int_neg(ineq->el[0], expanded[i].val);
2386 info->bmap = isl_basic_map_add_ineq(info->bmap,
2387 ineq->el);
2388 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2390 if (copy_ineq(info->tab, info->bmap) < 0)
2391 break;
2392 if (expanded[i].cst &&
2393 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2394 break;
2397 isl_vec_free(ineq);
2399 clear_status(info);
2400 init_status(info);
2402 return i < n ? isl_stat_error : isl_stat_ok;
2405 /* Insert the "n" integer division variables "expanded"
2406 * into info->tab and info->bmap and
2407 * update info->ineq with respect to the redundant constraints
2408 * in the resulting tableau.
2409 * "bmap" contains the result of this insertion in info->bmap,
2410 * while info->bmap is the original version
2411 * of "bmap", i.e., the one that corresponds to the current
2412 * state of info->tab. The number of constraints in info->bmap
2413 * is assumed to be the same as the number of constraints
2414 * in info->tab. This is required to be able to detect
2415 * the extra constraints in "bmap".
2417 * In particular, introduce extra variables corresponding
2418 * to the extra integer divisions and add the div constraints
2419 * that were added to "bmap" after info->tab was created
2420 * from info->bmap.
2421 * Furthermore, check if these extra integer divisions happen
2422 * to attain a fixed integer value in info->tab.
2423 * If so, replace the corresponding div constraints by pairs
2424 * of inequality constraints that fix these
2425 * integer divisions to their single integer values.
2426 * Replace info->bmap by "bmap" to match the changes to info->tab.
2427 * info->ineq was computed without a tableau and therefore
2428 * does not take into account the redundant constraints
2429 * in the tableau. Mark them here.
2430 * There is no need to check the newly added div constraints
2431 * since they cannot be redundant.
2432 * The redundancy check is not performed when constants have been discovered
2433 * since info->ineq is completely thrown away in this case.
2435 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2436 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2438 int i, n_ineq;
2439 unsigned n_eq;
2440 struct isl_tab_undo *snap;
2441 int any;
2443 if (!bmap)
2444 return isl_stat_error;
2445 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2446 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2447 "original tableau does not correspond "
2448 "to original basic map", goto error);
2450 if (isl_tab_extend_vars(info->tab, n) < 0)
2451 goto error;
2452 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2453 goto error;
2455 for (i = 0; i < n; ++i) {
2456 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2457 goto error;
2460 snap = isl_tab_snap(info->tab);
2462 n_ineq = info->tab->n_con - info->tab->n_eq;
2463 if (copy_ineq(info->tab, bmap) < 0)
2464 goto error;
2466 isl_basic_map_free(info->bmap);
2467 info->bmap = bmap;
2469 any = 0;
2470 for (i = 0; i < n; ++i) {
2471 expanded[i].cst = isl_tab_is_constant(info->tab,
2472 expanded[i].pos, &expanded[i].val);
2473 if (expanded[i].cst < 0)
2474 return isl_stat_error;
2475 if (expanded[i].cst)
2476 any = 1;
2479 if (any) {
2480 if (isl_tab_rollback(info->tab, snap) < 0)
2481 return isl_stat_error;
2482 info->bmap = isl_basic_map_cow(info->bmap);
2483 if (isl_basic_map_free_inequality(info->bmap, 2 * n) < 0)
2484 return isl_stat_error;
2486 return fix_constant_divs(info, n, expanded);
2489 n_eq = info->bmap->n_eq;
2490 for (i = 0; i < n_ineq; ++i) {
2491 if (isl_tab_is_redundant(info->tab, n_eq + i))
2492 info->ineq[i] = STATUS_REDUNDANT;
2495 return isl_stat_ok;
2496 error:
2497 isl_basic_map_free(bmap);
2498 return isl_stat_error;
2501 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2502 * in isl_basic_map_expand_divs using the expansion "exp" and
2503 * update info->ineq with respect to the redundant constraints
2504 * in the resulting tableau. info->bmap is the original version
2505 * of "bmap", i.e., the one that corresponds to the current
2506 * state of info->tab. The number of constraints in info->bmap
2507 * is assumed to be the same as the number of constraints
2508 * in info->tab. This is required to be able to detect
2509 * the extra constraints in "bmap".
2511 * Extract the positions where extra local variables are introduced
2512 * from "exp" and call tab_insert_divs.
2514 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2515 __isl_take isl_basic_map *bmap)
2517 isl_ctx *ctx;
2518 struct isl_expanded *expanded;
2519 int i, j, k, n;
2520 int extra_var;
2521 unsigned total, pos, n_div;
2522 isl_stat r;
2524 total = isl_basic_map_dim(bmap, isl_dim_all);
2525 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2526 pos = total - n_div;
2527 extra_var = total - info->tab->n_var;
2528 n = n_div - extra_var;
2530 ctx = isl_basic_map_get_ctx(bmap);
2531 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
2532 if (extra_var && !expanded)
2533 goto error;
2535 i = 0;
2536 k = 0;
2537 for (j = 0; j < n_div; ++j) {
2538 if (i < n && exp[i] == j) {
2539 ++i;
2540 continue;
2542 expanded[k++].pos = pos + j;
2545 for (k = 0; k < extra_var; ++k)
2546 isl_int_init(expanded[k].val);
2548 r = tab_insert_divs(info, extra_var, expanded, bmap);
2550 for (k = 0; k < extra_var; ++k)
2551 isl_int_clear(expanded[k].val);
2552 free(expanded);
2554 return r;
2555 error:
2556 isl_basic_map_free(bmap);
2557 return isl_stat_error;
2560 /* Check if the union of the basic maps represented by info[i] and info[j]
2561 * can be represented by a single basic map,
2562 * after expanding the divs of info[i] to match those of info[j].
2563 * If so, replace the pair by the single basic map and return
2564 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2565 * Otherwise, return isl_change_none.
2567 * The caller has already checked for info[j] being a subset of info[i].
2568 * If some of the divs of info[j] are unknown, then the expanded info[i]
2569 * will not have the corresponding div constraints. The other patterns
2570 * therefore cannot apply. Skip the computation in this case.
2572 * The expansion is performed using the divs "div" and expansion "exp"
2573 * computed by the caller.
2574 * info[i].bmap has already been expanded and the result is passed in
2575 * as "bmap".
2576 * The "eq" and "ineq" fields of info[i] reflect the status of
2577 * the constraints of the expanded "bmap" with respect to info[j].tab.
2578 * However, inequality constraints that are redundant in info[i].tab
2579 * have not yet been marked as such because no tableau was available.
2581 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2582 * updating info[i].ineq with respect to the redundant constraints.
2583 * Then try and coalesce the expanded info[i] with info[j],
2584 * reusing the information in info[i].eq and info[i].ineq.
2585 * If this does not result in any coalescing or if it results in info[j]
2586 * getting dropped (which should not happen in practice, since the case
2587 * of info[j] being a subset of info[i] has already been checked by
2588 * the caller), then revert info[i] to its original state.
2590 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2591 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2592 int *exp)
2594 isl_bool known;
2595 isl_basic_map *bmap_i;
2596 struct isl_tab_undo *snap;
2597 enum isl_change change = isl_change_none;
2599 known = isl_basic_map_divs_known(info[j].bmap);
2600 if (known < 0 || !known) {
2601 clear_status(&info[i]);
2602 isl_basic_map_free(bmap);
2603 return known < 0 ? isl_change_error : isl_change_none;
2606 bmap_i = isl_basic_map_copy(info[i].bmap);
2607 snap = isl_tab_snap(info[i].tab);
2608 if (expand_tab(&info[i], exp, bmap) < 0)
2609 change = isl_change_error;
2611 init_status(&info[j]);
2612 if (change == isl_change_none)
2613 change = coalesce_local_pair_reuse(i, j, info);
2614 else
2615 clear_status(&info[i]);
2616 if (change != isl_change_none && change != isl_change_drop_second) {
2617 isl_basic_map_free(bmap_i);
2618 } else {
2619 isl_basic_map_free(info[i].bmap);
2620 info[i].bmap = bmap_i;
2622 if (isl_tab_rollback(info[i].tab, snap) < 0)
2623 change = isl_change_error;
2626 return change;
2629 /* Check if the union of "bmap" and the basic map represented by info[j]
2630 * can be represented by a single basic map,
2631 * after expanding the divs of "bmap" to match those of info[j].
2632 * If so, replace the pair by the single basic map and return
2633 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2634 * Otherwise, return isl_change_none.
2636 * In particular, check if the expanded "bmap" contains the basic map
2637 * represented by the tableau info[j].tab.
2638 * The expansion is performed using the divs "div" and expansion "exp"
2639 * computed by the caller.
2640 * Then we check if all constraints of the expanded "bmap" are valid for
2641 * info[j].tab.
2643 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2644 * In this case, the positions of the constraints of info[i].bmap
2645 * with respect to the basic map represented by info[j] are stored
2646 * in info[i].
2648 * If the expanded "bmap" does not contain the basic map
2649 * represented by the tableau info[j].tab and if "i" is not -1,
2650 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2651 * as well and check if that results in coalescing.
2653 static enum isl_change coalesce_with_expanded_divs(
2654 __isl_keep isl_basic_map *bmap, int i, int j,
2655 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2657 enum isl_change change = isl_change_none;
2658 struct isl_coalesce_info info_local, *info_i;
2660 info_i = i >= 0 ? &info[i] : &info_local;
2661 init_status(info_i);
2662 bmap = isl_basic_map_copy(bmap);
2663 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2664 bmap = isl_basic_map_mark_final(bmap);
2666 if (!bmap)
2667 goto error;
2669 info_i->eq = eq_status_in(bmap, info[j].tab);
2670 if (bmap->n_eq && !info_i->eq)
2671 goto error;
2672 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2673 goto error;
2674 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2675 goto done;
2677 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2678 if (bmap->n_ineq && !info_i->ineq)
2679 goto error;
2680 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2681 goto error;
2682 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2683 goto done;
2685 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2686 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2687 drop(&info[j]);
2688 change = isl_change_drop_second;
2691 if (change == isl_change_none && i != -1)
2692 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2694 done:
2695 isl_basic_map_free(bmap);
2696 clear_status(info_i);
2697 return change;
2698 error:
2699 isl_basic_map_free(bmap);
2700 clear_status(info_i);
2701 return isl_change_error;
2704 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2705 * can be represented by a single basic map,
2706 * after aligning the divs of "bmap_i" to match those of info[j].
2707 * If so, replace the pair by the single basic map and return
2708 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2709 * Otherwise, return isl_change_none.
2711 * In particular, check if "bmap_i" contains the basic map represented by
2712 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2713 * Note that this can only succeed if the number of divs of "bmap_i"
2714 * is smaller than (or equal to) the number of divs of info[j].
2716 * We first check if the divs of "bmap_i" are all known and form a subset
2717 * of those of info[j].bmap. If so, we pass control over to
2718 * coalesce_with_expanded_divs.
2720 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2722 static enum isl_change coalesce_after_aligning_divs(
2723 __isl_keep isl_basic_map *bmap_i, int i, int j,
2724 struct isl_coalesce_info *info)
2726 int known;
2727 isl_mat *div_i, *div_j, *div;
2728 int *exp1 = NULL;
2729 int *exp2 = NULL;
2730 isl_ctx *ctx;
2731 enum isl_change change;
2733 known = isl_basic_map_divs_known(bmap_i);
2734 if (known < 0 || !known)
2735 return known;
2737 ctx = isl_basic_map_get_ctx(bmap_i);
2739 div_i = isl_basic_map_get_divs(bmap_i);
2740 div_j = isl_basic_map_get_divs(info[j].bmap);
2742 if (!div_i || !div_j)
2743 goto error;
2745 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2746 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2747 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2748 goto error;
2750 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2751 if (!div)
2752 goto error;
2754 if (div->n_row == div_j->n_row)
2755 change = coalesce_with_expanded_divs(bmap_i,
2756 i, j, info, div, exp1);
2757 else
2758 change = isl_change_none;
2760 isl_mat_free(div);
2762 isl_mat_free(div_i);
2763 isl_mat_free(div_j);
2765 free(exp2);
2766 free(exp1);
2768 return change;
2769 error:
2770 isl_mat_free(div_i);
2771 isl_mat_free(div_j);
2772 free(exp1);
2773 free(exp2);
2774 return isl_change_error;
2777 /* Check if basic map "j" is a subset of basic map "i" after
2778 * exploiting the extra equalities of "j" to simplify the divs of "i".
2779 * If so, remove basic map "j" and return isl_change_drop_second.
2781 * If "j" does not have any equalities or if they are the same
2782 * as those of "i", then we cannot exploit them to simplify the divs.
2783 * Similarly, if there are no divs in "i", then they cannot be simplified.
2784 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2785 * then "j" cannot be a subset of "i".
2787 * Otherwise, we intersect "i" with the affine hull of "j" and then
2788 * check if "j" is a subset of the result after aligning the divs.
2789 * If so, then "j" is definitely a subset of "i" and can be removed.
2790 * Note that if after intersection with the affine hull of "j".
2791 * "i" still has more divs than "j", then there is no way we can
2792 * align the divs of "i" to those of "j".
2794 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2795 struct isl_coalesce_info *info)
2797 isl_basic_map *hull_i, *hull_j, *bmap_i;
2798 int equal, empty;
2799 enum isl_change change;
2801 if (info[j].bmap->n_eq == 0)
2802 return isl_change_none;
2803 if (info[i].bmap->n_div == 0)
2804 return isl_change_none;
2806 hull_i = isl_basic_map_copy(info[i].bmap);
2807 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2808 hull_j = isl_basic_map_copy(info[j].bmap);
2809 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2811 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2812 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2813 empty = isl_basic_map_plain_is_empty(hull_j);
2814 isl_basic_map_free(hull_i);
2816 if (equal < 0 || equal || empty < 0 || empty) {
2817 isl_basic_map_free(hull_j);
2818 if (equal < 0 || empty < 0)
2819 return isl_change_error;
2820 return isl_change_none;
2823 bmap_i = isl_basic_map_copy(info[i].bmap);
2824 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2825 if (!bmap_i)
2826 return isl_change_error;
2828 if (bmap_i->n_div > info[j].bmap->n_div) {
2829 isl_basic_map_free(bmap_i);
2830 return isl_change_none;
2833 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2835 isl_basic_map_free(bmap_i);
2837 return change;
2840 /* Check if the union of and the basic maps represented by info[i] and info[j]
2841 * can be represented by a single basic map, by aligning or equating
2842 * their integer divisions.
2843 * If so, replace the pair by the single basic map and return
2844 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2845 * Otherwise, return isl_change_none.
2847 * Note that we only perform any test if the number of divs is different
2848 * in the two basic maps. In case the number of divs is the same,
2849 * we have already established that the divs are different
2850 * in the two basic maps.
2851 * In particular, if the number of divs of basic map i is smaller than
2852 * the number of divs of basic map j, then we check if j is a subset of i
2853 * and vice versa.
2855 static enum isl_change coalesce_divs(int i, int j,
2856 struct isl_coalesce_info *info)
2858 enum isl_change change = isl_change_none;
2860 if (info[i].bmap->n_div < info[j].bmap->n_div)
2861 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2862 if (change != isl_change_none)
2863 return change;
2865 if (info[j].bmap->n_div < info[i].bmap->n_div)
2866 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2867 if (change != isl_change_none)
2868 return invert_change(change);
2870 change = coalesce_subset_with_equalities(i, j, info);
2871 if (change != isl_change_none)
2872 return change;
2874 change = coalesce_subset_with_equalities(j, i, info);
2875 if (change != isl_change_none)
2876 return invert_change(change);
2878 return isl_change_none;
2881 /* Does "bmap" involve any divs that themselves refer to divs?
2883 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2885 int i;
2886 unsigned total;
2887 unsigned n_div;
2889 total = isl_basic_map_dim(bmap, isl_dim_all);
2890 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2891 total -= n_div;
2893 for (i = 0; i < n_div; ++i)
2894 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2895 n_div) != -1)
2896 return 1;
2898 return 0;
2901 /* Return a list of affine expressions, one for each integer division
2902 * in "bmap_i". For each integer division that also appears in "bmap_j",
2903 * the affine expression is set to NaN. The number of NaNs in the list
2904 * is equal to the number of integer divisions in "bmap_j".
2905 * For the other integer divisions of "bmap_i", the corresponding
2906 * element in the list is a purely affine expression equal to the integer
2907 * division in "hull".
2908 * If no such list can be constructed, then the number of elements
2909 * in the returned list is smaller than the number of integer divisions
2910 * in "bmap_i".
2912 static __isl_give isl_aff_list *set_up_substitutions(
2913 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2914 __isl_take isl_basic_map *hull)
2916 unsigned n_div_i, n_div_j, total;
2917 isl_ctx *ctx;
2918 isl_local_space *ls;
2919 isl_basic_set *wrap_hull;
2920 isl_aff *aff_nan;
2921 isl_aff_list *list;
2922 int i, j;
2924 if (!hull)
2925 return NULL;
2927 ctx = isl_basic_map_get_ctx(hull);
2929 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2930 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2931 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2933 ls = isl_basic_map_get_local_space(bmap_i);
2934 ls = isl_local_space_wrap(ls);
2935 wrap_hull = isl_basic_map_wrap(hull);
2937 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2938 list = isl_aff_list_alloc(ctx, n_div_i);
2940 j = 0;
2941 for (i = 0; i < n_div_i; ++i) {
2942 isl_aff *aff;
2944 if (j < n_div_j &&
2945 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2946 ++j;
2947 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2948 continue;
2950 if (n_div_i - i <= n_div_j - j)
2951 break;
2953 aff = isl_local_space_get_div(ls, i);
2954 aff = isl_aff_substitute_equalities(aff,
2955 isl_basic_set_copy(wrap_hull));
2956 aff = isl_aff_floor(aff);
2957 if (!aff)
2958 goto error;
2959 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2960 isl_aff_free(aff);
2961 break;
2964 list = isl_aff_list_add(list, aff);
2967 isl_aff_free(aff_nan);
2968 isl_local_space_free(ls);
2969 isl_basic_set_free(wrap_hull);
2971 return list;
2972 error:
2973 isl_aff_free(aff_nan);
2974 isl_local_space_free(ls);
2975 isl_basic_set_free(wrap_hull);
2976 isl_aff_list_free(list);
2977 return NULL;
2980 /* Add variables to info->bmap and info->tab corresponding to the elements
2981 * in "list" that are not set to NaN.
2982 * "extra_var" is the number of these elements.
2983 * "dim" is the offset in the variables of "tab" where we should
2984 * start considering the elements in "list".
2985 * When this function returns, the total number of variables in "tab"
2986 * is equal to "dim" plus the number of elements in "list".
2988 * The newly added existentially quantified variables are not given
2989 * an explicit representation because the corresponding div constraints
2990 * do not appear in info->bmap. These constraints are not added
2991 * to info->bmap because for internal consistency, they would need to
2992 * be added to info->tab as well, where they could combine with the equality
2993 * that is added later to result in constraints that do not hold
2994 * in the original input.
2996 static int add_sub_vars(struct isl_coalesce_info *info,
2997 __isl_keep isl_aff_list *list, int dim, int extra_var)
2999 int i, j, n, d;
3000 isl_space *space;
3002 space = isl_basic_map_get_space(info->bmap);
3003 info->bmap = isl_basic_map_cow(info->bmap);
3004 info->bmap = isl_basic_map_extend_space(info->bmap, space,
3005 extra_var, 0, 0);
3006 if (!info->bmap)
3007 return -1;
3008 n = isl_aff_list_n_aff(list);
3009 for (i = 0; i < n; ++i) {
3010 int is_nan;
3011 isl_aff *aff;
3013 aff = isl_aff_list_get_aff(list, i);
3014 is_nan = isl_aff_is_nan(aff);
3015 isl_aff_free(aff);
3016 if (is_nan < 0)
3017 return -1;
3018 if (is_nan)
3019 continue;
3021 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3022 return -1;
3023 d = isl_basic_map_alloc_div(info->bmap);
3024 if (d < 0)
3025 return -1;
3026 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3027 if (!info->bmap)
3028 return -1;
3029 for (j = d; j > i; --j)
3030 isl_basic_map_swap_div(info->bmap, j - 1, j);
3033 return 0;
3036 /* For each element in "list" that is not set to NaN, fix the corresponding
3037 * variable in "tab" to the purely affine expression defined by the element.
3038 * "dim" is the offset in the variables of "tab" where we should
3039 * start considering the elements in "list".
3041 * This function assumes that a sufficient number of rows and
3042 * elements in the constraint array are available in the tableau.
3044 static int add_sub_equalities(struct isl_tab *tab,
3045 __isl_keep isl_aff_list *list, int dim)
3047 int i, n;
3048 isl_ctx *ctx;
3049 isl_vec *sub;
3050 isl_aff *aff;
3052 n = isl_aff_list_n_aff(list);
3054 ctx = isl_tab_get_ctx(tab);
3055 sub = isl_vec_alloc(ctx, 1 + dim + n);
3056 if (!sub)
3057 return -1;
3058 isl_seq_clr(sub->el + 1 + dim, n);
3060 for (i = 0; i < n; ++i) {
3061 aff = isl_aff_list_get_aff(list, i);
3062 if (!aff)
3063 goto error;
3064 if (isl_aff_is_nan(aff)) {
3065 isl_aff_free(aff);
3066 continue;
3068 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3069 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3070 if (isl_tab_add_eq(tab, sub->el) < 0)
3071 goto error;
3072 isl_int_set_si(sub->el[1 + dim + i], 0);
3073 isl_aff_free(aff);
3076 isl_vec_free(sub);
3077 return 0;
3078 error:
3079 isl_aff_free(aff);
3080 isl_vec_free(sub);
3081 return -1;
3084 /* Add variables to info->tab and info->bmap corresponding to the elements
3085 * in "list" that are not set to NaN. The value of the added variable
3086 * in info->tab is fixed to the purely affine expression defined by the element.
3087 * "dim" is the offset in the variables of info->tab where we should
3088 * start considering the elements in "list".
3089 * When this function returns, the total number of variables in info->tab
3090 * is equal to "dim" plus the number of elements in "list".
3092 static int add_subs(struct isl_coalesce_info *info,
3093 __isl_keep isl_aff_list *list, int dim)
3095 int extra_var;
3096 int n;
3098 if (!list)
3099 return -1;
3101 n = isl_aff_list_n_aff(list);
3102 extra_var = n - (info->tab->n_var - dim);
3104 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3105 return -1;
3106 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3107 return -1;
3108 if (add_sub_vars(info, list, dim, extra_var) < 0)
3109 return -1;
3111 return add_sub_equalities(info->tab, list, dim);
3114 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3115 * divisions in "i" but not in "j" to basic map "j", with values
3116 * specified by "list". The total number of elements in "list"
3117 * is equal to the number of integer divisions in "i", while the number
3118 * of NaN elements in the list is equal to the number of integer divisions
3119 * in "j".
3121 * If no coalescing can be performed, then we need to revert basic map "j"
3122 * to its original state. We do the same if basic map "i" gets dropped
3123 * during the coalescing, even though this should not happen in practice
3124 * since we have already checked for "j" being a subset of "i"
3125 * before we reach this stage.
3127 static enum isl_change coalesce_with_subs(int i, int j,
3128 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3130 isl_basic_map *bmap_j;
3131 struct isl_tab_undo *snap;
3132 unsigned dim;
3133 enum isl_change change;
3135 bmap_j = isl_basic_map_copy(info[j].bmap);
3136 snap = isl_tab_snap(info[j].tab);
3138 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3139 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
3140 if (add_subs(&info[j], list, dim) < 0)
3141 goto error;
3143 change = coalesce_local_pair(i, j, info);
3144 if (change != isl_change_none && change != isl_change_drop_first) {
3145 isl_basic_map_free(bmap_j);
3146 } else {
3147 isl_basic_map_free(info[j].bmap);
3148 info[j].bmap = bmap_j;
3150 if (isl_tab_rollback(info[j].tab, snap) < 0)
3151 return isl_change_error;
3154 return change;
3155 error:
3156 isl_basic_map_free(bmap_j);
3157 return isl_change_error;
3160 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3161 * those extra integer divisions in "i" that can be simplified away
3162 * using the extra equalities in "j".
3163 * All divs are assumed to be known and not contain any nested divs.
3165 * We first check if there are any extra equalities in "j" that we
3166 * can exploit. Then we check if every integer division in "i"
3167 * either already appears in "j" or can be simplified using the
3168 * extra equalities to a purely affine expression.
3169 * If these tests succeed, then we try to coalesce the two basic maps
3170 * by introducing extra dimensions in "j" corresponding to
3171 * the extra integer divsisions "i" fixed to the corresponding
3172 * purely affine expression.
3174 static enum isl_change check_coalesce_into_eq(int i, int j,
3175 struct isl_coalesce_info *info)
3177 unsigned n_div_i, n_div_j;
3178 isl_basic_map *hull_i, *hull_j;
3179 int equal, empty;
3180 isl_aff_list *list;
3181 enum isl_change change;
3183 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3184 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3185 if (n_div_i <= n_div_j)
3186 return isl_change_none;
3187 if (info[j].bmap->n_eq == 0)
3188 return isl_change_none;
3190 hull_i = isl_basic_map_copy(info[i].bmap);
3191 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3192 hull_j = isl_basic_map_copy(info[j].bmap);
3193 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3195 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3196 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3197 empty = isl_basic_map_plain_is_empty(hull_j);
3198 isl_basic_map_free(hull_i);
3200 if (equal < 0 || empty < 0)
3201 goto error;
3202 if (equal || empty) {
3203 isl_basic_map_free(hull_j);
3204 return isl_change_none;
3207 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3208 if (!list)
3209 return isl_change_error;
3210 if (isl_aff_list_n_aff(list) < n_div_i)
3211 change = isl_change_none;
3212 else
3213 change = coalesce_with_subs(i, j, info, list);
3215 isl_aff_list_free(list);
3217 return change;
3218 error:
3219 isl_basic_map_free(hull_j);
3220 return isl_change_error;
3223 /* Check if we can coalesce basic maps "i" and "j" after copying
3224 * those extra integer divisions in one of the basic maps that can
3225 * be simplified away using the extra equalities in the other basic map.
3226 * We require all divs to be known in both basic maps.
3227 * Furthermore, to simplify the comparison of div expressions,
3228 * we do not allow any nested integer divisions.
3230 static enum isl_change check_coalesce_eq(int i, int j,
3231 struct isl_coalesce_info *info)
3233 int known, nested;
3234 enum isl_change change;
3236 known = isl_basic_map_divs_known(info[i].bmap);
3237 if (known < 0 || !known)
3238 return known < 0 ? isl_change_error : isl_change_none;
3239 known = isl_basic_map_divs_known(info[j].bmap);
3240 if (known < 0 || !known)
3241 return known < 0 ? isl_change_error : isl_change_none;
3242 nested = has_nested_div(info[i].bmap);
3243 if (nested < 0 || nested)
3244 return nested < 0 ? isl_change_error : isl_change_none;
3245 nested = has_nested_div(info[j].bmap);
3246 if (nested < 0 || nested)
3247 return nested < 0 ? isl_change_error : isl_change_none;
3249 change = check_coalesce_into_eq(i, j, info);
3250 if (change != isl_change_none)
3251 return change;
3252 change = check_coalesce_into_eq(j, i, info);
3253 if (change != isl_change_none)
3254 return invert_change(change);
3256 return isl_change_none;
3259 /* Check if the union of the given pair of basic maps
3260 * can be represented by a single basic map.
3261 * If so, replace the pair by the single basic map and return
3262 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3263 * Otherwise, return isl_change_none.
3265 * We first check if the two basic maps live in the same local space,
3266 * after aligning the divs that differ by only an integer constant.
3267 * If so, we do the complete check. Otherwise, we check if they have
3268 * the same number of integer divisions and can be coalesced, if one is
3269 * an obvious subset of the other or if the extra integer divisions
3270 * of one basic map can be simplified away using the extra equalities
3271 * of the other basic map.
3273 static enum isl_change coalesce_pair(int i, int j,
3274 struct isl_coalesce_info *info)
3276 int same;
3277 enum isl_change change;
3279 if (harmonize_divs(&info[i], &info[j]) < 0)
3280 return isl_change_error;
3281 same = same_divs(info[i].bmap, info[j].bmap);
3282 if (same < 0)
3283 return isl_change_error;
3284 if (same)
3285 return coalesce_local_pair(i, j, info);
3287 if (info[i].bmap->n_div == info[j].bmap->n_div) {
3288 change = coalesce_local_pair(i, j, info);
3289 if (change != isl_change_none)
3290 return change;
3293 change = coalesce_divs(i, j, info);
3294 if (change != isl_change_none)
3295 return change;
3297 return check_coalesce_eq(i, j, info);
3300 /* Return the maximum of "a" and "b".
3302 static int isl_max(int a, int b)
3304 return a > b ? a : b;
3307 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3308 * with those in the range [start2, end2[, skipping basic maps
3309 * that have been removed (either before or within this function).
3311 * For each basic map i in the first range, we check if it can be coalesced
3312 * with respect to any previously considered basic map j in the second range.
3313 * If i gets dropped (because it was a subset of some j), then
3314 * we can move on to the next basic map.
3315 * If j gets dropped, we need to continue checking against the other
3316 * previously considered basic maps.
3317 * If the two basic maps got fused, then we recheck the fused basic map
3318 * against the previously considered basic maps, starting at i + 1
3319 * (even if start2 is greater than i + 1).
3321 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3322 int start1, int end1, int start2, int end2)
3324 int i, j;
3326 for (i = end1 - 1; i >= start1; --i) {
3327 if (info[i].removed)
3328 continue;
3329 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3330 enum isl_change changed;
3332 if (info[j].removed)
3333 continue;
3334 if (info[i].removed)
3335 isl_die(ctx, isl_error_internal,
3336 "basic map unexpectedly removed",
3337 return -1);
3338 changed = coalesce_pair(i, j, info);
3339 switch (changed) {
3340 case isl_change_error:
3341 return -1;
3342 case isl_change_none:
3343 case isl_change_drop_second:
3344 continue;
3345 case isl_change_drop_first:
3346 j = end2;
3347 break;
3348 case isl_change_fuse:
3349 j = i;
3350 break;
3355 return 0;
3358 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3360 * We consider groups of basic maps that live in the same apparent
3361 * affine hull and we first coalesce within such a group before we
3362 * coalesce the elements in the group with elements of previously
3363 * considered groups. If a fuse happens during the second phase,
3364 * then we also reconsider the elements within the group.
3366 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3368 int start, end;
3370 for (end = n; end > 0; end = start) {
3371 start = end - 1;
3372 while (start >= 1 &&
3373 info[start - 1].hull_hash == info[start].hull_hash)
3374 start--;
3375 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3376 return -1;
3377 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3378 return -1;
3381 return 0;
3384 /* Update the basic maps in "map" based on the information in "info".
3385 * In particular, remove the basic maps that have been marked removed and
3386 * update the others based on the information in the corresponding tableau.
3387 * Since we detected implicit equalities without calling
3388 * isl_basic_map_gauss, we need to do it now.
3389 * Also call isl_basic_map_simplify if we may have lost the definition
3390 * of one or more integer divisions.
3392 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3393 int n, struct isl_coalesce_info *info)
3395 int i;
3397 if (!map)
3398 return NULL;
3400 for (i = n - 1; i >= 0; --i) {
3401 if (info[i].removed) {
3402 isl_basic_map_free(map->p[i]);
3403 if (i != map->n - 1)
3404 map->p[i] = map->p[map->n - 1];
3405 map->n--;
3406 continue;
3409 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3410 info[i].tab);
3411 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3412 if (info[i].simplify)
3413 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3414 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3415 if (!info[i].bmap)
3416 return isl_map_free(map);
3417 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3418 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3419 isl_basic_map_free(map->p[i]);
3420 map->p[i] = info[i].bmap;
3421 info[i].bmap = NULL;
3424 return map;
3427 /* For each pair of basic maps in the map, check if the union of the two
3428 * can be represented by a single basic map.
3429 * If so, replace the pair by the single basic map and start over.
3431 * We factor out any (hidden) common factor from the constraint
3432 * coefficients to improve the detection of adjacent constraints.
3434 * Since we are constructing the tableaus of the basic maps anyway,
3435 * we exploit them to detect implicit equalities and redundant constraints.
3436 * This also helps the coalescing as it can ignore the redundant constraints.
3437 * In order to avoid confusion, we make all implicit equalities explicit
3438 * in the basic maps. We don't call isl_basic_map_gauss, though,
3439 * as that may affect the number of constraints.
3440 * This means that we have to call isl_basic_map_gauss at the end
3441 * of the computation (in update_basic_maps) to ensure that
3442 * the basic maps are not left in an unexpected state.
3443 * For each basic map, we also compute the hash of the apparent affine hull
3444 * for use in coalesce.
3446 struct isl_map *isl_map_coalesce(struct isl_map *map)
3448 int i;
3449 unsigned n;
3450 isl_ctx *ctx;
3451 struct isl_coalesce_info *info = NULL;
3453 map = isl_map_remove_empty_parts(map);
3454 if (!map)
3455 return NULL;
3457 if (map->n <= 1)
3458 return map;
3460 ctx = isl_map_get_ctx(map);
3461 map = isl_map_sort_divs(map);
3462 map = isl_map_cow(map);
3464 if (!map)
3465 return NULL;
3467 n = map->n;
3469 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3470 if (!info)
3471 goto error;
3473 for (i = 0; i < map->n; ++i) {
3474 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3475 if (!map->p[i])
3476 goto error;
3477 info[i].bmap = isl_basic_map_copy(map->p[i]);
3478 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3479 if (!info[i].tab)
3480 goto error;
3481 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3482 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3483 goto error;
3484 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3485 info[i].bmap);
3486 if (!info[i].bmap)
3487 goto error;
3488 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3489 if (isl_tab_detect_redundant(info[i].tab) < 0)
3490 goto error;
3491 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3492 goto error;
3494 for (i = map->n - 1; i >= 0; --i)
3495 if (info[i].tab->empty)
3496 drop(&info[i]);
3498 if (coalesce(ctx, n, info) < 0)
3499 goto error;
3501 map = update_basic_maps(map, n, info);
3503 clear_coalesce_info(n, info);
3505 return map;
3506 error:
3507 clear_coalesce_info(n, info);
3508 isl_map_free(map);
3509 return NULL;
3512 /* For each pair of basic sets in the set, check if the union of the two
3513 * can be represented by a single basic set.
3514 * If so, replace the pair by the single basic set and start over.
3516 struct isl_set *isl_set_coalesce(struct isl_set *set)
3518 return set_from_map(isl_map_coalesce(set_to_map(set)));