isl_coalesce.c: extract out shared find function
[isl.git] / isl_coalesce.c
blobf87a3b984c253bb8c2045af718afae21acb30dcc
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 replacing constraint k of info->bmap, i.e., f_k >= 0,
871 * by the adjacent equality, i.e., f_k + 1 = 0.
873 * Compute a variable compression from the equality constraint f_k + 1 = 0
874 * and use it to tighten the other constraints of info->bmap,
875 * updating info->tab (and leaving info->bmap untouched).
876 * The compression handles essentially two cases, one where a variable
877 * is assigned a fixed value and can therefore be eliminated, and one
878 * where one variable is a shifted multiple of some other variable and
879 * can therefore be replaced by that multiple.
880 * Gaussian elimination would also work for the first case, but for
881 * the second case, the effectiveness would depend on the order
882 * of the variables.
883 * After compression, some of the constraints may have coefficients
884 * with a common divisor. If this divisor does not divide the constant
885 * term, then the constraint can be tightened.
886 * The tightening is performed on the tableau info->tab by introducing
887 * extra (temporary) constraints.
889 * Only constraints that are possibly affected by the compression are
890 * considered. In particular, if the constraint only involves variables
891 * that are directly mapped to a distinct set of other variables, then
892 * no common divisor can be introduced and no tightening can occur.
894 * It is important to only consider the non-redundant constraints
895 * since the facet constraint has been relaxed prior to the call
896 * to this function, meaning that the constraints that were redundant
897 * prior to the relaxation may no longer be redundant.
898 * These constraints will be ignored in the fused result, so
899 * the fusion detection should not exploit them.
901 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
902 int k)
904 unsigned total;
905 isl_ctx *ctx;
906 isl_vec *v = NULL;
907 isl_mat *T;
908 int i;
909 int *affected;
911 ctx = isl_basic_map_get_ctx(info->bmap);
912 total = isl_basic_map_total_dim(info->bmap);
913 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
914 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
915 T = isl_mat_variable_compression(T, NULL);
916 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
917 if (!T)
918 return isl_stat_error;
919 if (T->n_col == 0) {
920 isl_mat_free(T);
921 return isl_stat_ok;
924 affected = isl_alloc_array(ctx, int, total);
925 if (!affected)
926 goto error;
928 for (i = 0; i < total; ++i)
929 affected[i] = not_unique_unit_row(T, 1 + i);
931 for (i = 0; i < info->bmap->n_ineq; ++i) {
932 if (i == k)
933 continue;
934 if (info->ineq[i] == STATUS_REDUNDANT)
935 continue;
936 if (!is_affected(info->bmap, i, affected, total))
937 continue;
938 v = isl_vec_alloc(ctx, 1 + total);
939 if (!v)
940 goto error;
941 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
942 v = isl_vec_mat_product(v, isl_mat_copy(T));
943 v = try_tightening(info, i, v);
944 isl_vec_free(v);
945 if (!v)
946 goto error;
949 isl_mat_free(T);
950 free(affected);
951 return isl_stat_ok;
952 error:
953 isl_mat_free(T);
954 free(affected);
955 return isl_stat_error;
958 /* Basic map "i" has an inequality "k" that is adjacent to some equality
959 * of basic map "j". All the other inequalities are valid for "j".
960 * Check if basic map "j" forms an extension of basic map "i".
962 * In particular, we relax constraint "k", compute the corresponding
963 * facet and check whether it is included in the other basic map.
964 * Before testing for inclusion, the constraints on the facet
965 * are tightened to increase the chance of an inclusion being detected.
966 * If the facet is included, we know that relaxing the constraint extends
967 * the basic map with exactly the other basic map (we already know that this
968 * other basic map is included in the extension, because there
969 * were no "cut" inequalities in "i") and we can replace the
970 * two basic maps by this extension.
971 * Each integer division that does not have exactly the same
972 * definition in "i" and "j" is marked unknown and the basic map
973 * is scheduled to be simplified in an attempt to recover
974 * the integer division definition.
975 * Place this extension in the position that is the smallest of i and j.
976 * ____ _____
977 * / || / |
978 * / || / |
979 * \ || => \ |
980 * \ || \ |
981 * \___|| \____|
983 static enum isl_change is_adj_eq_extension(int i, int j, int k,
984 struct isl_coalesce_info *info)
986 int change = isl_change_none;
987 int super;
988 struct isl_tab_undo *snap, *snap2;
989 unsigned n_eq = info[i].bmap->n_eq;
991 if (isl_tab_is_equality(info[i].tab, n_eq + k))
992 return isl_change_none;
994 snap = isl_tab_snap(info[i].tab);
995 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
996 return isl_change_error;
997 snap2 = isl_tab_snap(info[i].tab);
998 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
999 return isl_change_error;
1000 if (tighten_on_relaxed_facet(&info[i], k) < 0)
1001 return isl_change_error;
1002 super = contains(&info[j], info[i].tab);
1003 if (super < 0)
1004 return isl_change_error;
1005 if (super) {
1006 int l;
1007 unsigned total;
1009 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1010 return isl_change_error;
1011 info[i].bmap = isl_basic_map_cow(info[i].bmap);
1012 if (!info[i].bmap)
1013 return isl_change_error;
1014 total = isl_basic_map_total_dim(info[i].bmap);
1015 for (l = 0; l < info[i].bmap->n_div; ++l)
1016 if (!isl_seq_eq(info[i].bmap->div[l],
1017 info[j].bmap->div[l], 1 + 1 + total)) {
1018 isl_int_set_si(info[i].bmap->div[l][0], 0);
1019 info[i].simplify = 1;
1021 isl_int_add_ui(info[i].bmap->ineq[k][0],
1022 info[i].bmap->ineq[k][0], 1);
1023 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1024 drop(&info[j]);
1025 if (j < i)
1026 exchange(&info[i], &info[j]);
1027 change = isl_change_fuse;
1028 } else
1029 if (isl_tab_rollback(info[i].tab, snap) < 0)
1030 return isl_change_error;
1032 return change;
1035 /* Data structure that keeps track of the wrapping constraints
1036 * and of information to bound the coefficients of those constraints.
1038 * bound is set if we want to apply a bound on the coefficients
1039 * mat contains the wrapping constraints
1040 * max is the bound on the coefficients (if bound is set)
1042 struct isl_wraps {
1043 int bound;
1044 isl_mat *mat;
1045 isl_int max;
1048 /* Update wraps->max to be greater than or equal to the coefficients
1049 * in the equalities and inequalities of info->bmap that can be removed
1050 * if we end up applying wrapping.
1052 static void wraps_update_max(struct isl_wraps *wraps,
1053 struct isl_coalesce_info *info)
1055 int k;
1056 isl_int max_k;
1057 unsigned total = isl_basic_map_total_dim(info->bmap);
1059 isl_int_init(max_k);
1061 for (k = 0; k < info->bmap->n_eq; ++k) {
1062 if (info->eq[2 * k] == STATUS_VALID &&
1063 info->eq[2 * k + 1] == STATUS_VALID)
1064 continue;
1065 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1066 if (isl_int_abs_gt(max_k, wraps->max))
1067 isl_int_set(wraps->max, max_k);
1070 for (k = 0; k < info->bmap->n_ineq; ++k) {
1071 if (info->ineq[k] == STATUS_VALID ||
1072 info->ineq[k] == STATUS_REDUNDANT)
1073 continue;
1074 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1075 if (isl_int_abs_gt(max_k, wraps->max))
1076 isl_int_set(wraps->max, max_k);
1079 isl_int_clear(max_k);
1082 /* Initialize the isl_wraps data structure.
1083 * If we want to bound the coefficients of the wrapping constraints,
1084 * we set wraps->max to the largest coefficient
1085 * in the equalities and inequalities that can be removed if we end up
1086 * applying wrapping.
1088 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1089 struct isl_coalesce_info *info, int i, int j)
1091 isl_ctx *ctx;
1093 wraps->bound = 0;
1094 wraps->mat = mat;
1095 if (!mat)
1096 return;
1097 ctx = isl_mat_get_ctx(mat);
1098 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1099 if (!wraps->bound)
1100 return;
1101 isl_int_init(wraps->max);
1102 isl_int_set_si(wraps->max, 0);
1103 wraps_update_max(wraps, &info[i]);
1104 wraps_update_max(wraps, &info[j]);
1107 /* Free the contents of the isl_wraps data structure.
1109 static void wraps_free(struct isl_wraps *wraps)
1111 isl_mat_free(wraps->mat);
1112 if (wraps->bound)
1113 isl_int_clear(wraps->max);
1116 /* Is the wrapping constraint in row "row" allowed?
1118 * If wraps->bound is set, we check that none of the coefficients
1119 * is greater than wraps->max.
1121 static int allow_wrap(struct isl_wraps *wraps, int row)
1123 int i;
1125 if (!wraps->bound)
1126 return 1;
1128 for (i = 1; i < wraps->mat->n_col; ++i)
1129 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1130 return 0;
1132 return 1;
1135 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1136 * to include "set" and add the result in position "w" of "wraps".
1137 * "len" is the total number of coefficients in "bound" and "ineq".
1138 * Return 1 on success, 0 on failure and -1 on error.
1139 * Wrapping can fail if the result of wrapping is equal to "bound"
1140 * or if we want to bound the sizes of the coefficients and
1141 * the wrapped constraint does not satisfy this bound.
1143 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1144 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1146 isl_seq_cpy(wraps->mat->row[w], bound, len);
1147 if (negate) {
1148 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1149 ineq = wraps->mat->row[w + 1];
1151 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1152 return -1;
1153 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1154 return 0;
1155 if (!allow_wrap(wraps, w))
1156 return 0;
1157 return 1;
1160 /* For each constraint in info->bmap that is not redundant (as determined
1161 * by info->tab) and that is not a valid constraint for the other basic map,
1162 * wrap the constraint around "bound" such that it includes the whole
1163 * set "set" and append the resulting constraint to "wraps".
1164 * Note that the constraints that are valid for the other basic map
1165 * will be added to the combined basic map by default, so there is
1166 * no need to wrap them.
1167 * The caller wrap_in_facets even relies on this function not wrapping
1168 * any constraints that are already valid.
1169 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1170 * wraps->n_row is the number of actual wrapped constraints that have
1171 * been added.
1172 * If any of the wrapping problems results in a constraint that is
1173 * identical to "bound", then this means that "set" is unbounded in such
1174 * way that no wrapping is possible. If this happens then wraps->n_row
1175 * is reset to zero.
1176 * Similarly, if we want to bound the coefficients of the wrapping
1177 * constraints and a newly added wrapping constraint does not
1178 * satisfy the bound, then wraps->n_row is also reset to zero.
1180 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1181 isl_int *bound, __isl_keep isl_set *set)
1183 int l, m;
1184 int w;
1185 int added;
1186 isl_basic_map *bmap = info->bmap;
1187 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1189 w = wraps->mat->n_row;
1191 for (l = 0; l < bmap->n_ineq; ++l) {
1192 if (info->ineq[l] == STATUS_VALID ||
1193 info->ineq[l] == STATUS_REDUNDANT)
1194 continue;
1195 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1196 continue;
1197 if (isl_seq_eq(bound, bmap->ineq[l], len))
1198 continue;
1199 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1200 continue;
1202 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1203 if (added < 0)
1204 return -1;
1205 if (!added)
1206 goto unbounded;
1207 ++w;
1209 for (l = 0; l < bmap->n_eq; ++l) {
1210 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1211 continue;
1212 if (isl_seq_eq(bound, bmap->eq[l], len))
1213 continue;
1215 for (m = 0; m < 2; ++m) {
1216 if (info->eq[2 * l + m] == STATUS_VALID)
1217 continue;
1218 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1219 set, !m);
1220 if (added < 0)
1221 return -1;
1222 if (!added)
1223 goto unbounded;
1224 ++w;
1228 wraps->mat->n_row = w;
1229 return 0;
1230 unbounded:
1231 wraps->mat->n_row = 0;
1232 return 0;
1235 /* Check if the constraints in "wraps" from "first" until the last
1236 * are all valid for the basic set represented by "tab".
1237 * If not, wraps->n_row is set to zero.
1239 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1240 struct isl_tab *tab)
1242 int i;
1244 for (i = first; i < wraps->n_row; ++i) {
1245 enum isl_ineq_type type;
1246 type = isl_tab_ineq_type(tab, wraps->row[i]);
1247 if (type == isl_ineq_error)
1248 return -1;
1249 if (type == isl_ineq_redundant)
1250 continue;
1251 wraps->n_row = 0;
1252 return 0;
1255 return 0;
1258 /* Return a set that corresponds to the non-redundant constraints
1259 * (as recorded in tab) of bmap.
1261 * It's important to remove the redundant constraints as some
1262 * of the other constraints may have been modified after the
1263 * constraints were marked redundant.
1264 * In particular, a constraint may have been relaxed.
1265 * Redundant constraints are ignored when a constraint is relaxed
1266 * and should therefore continue to be ignored ever after.
1267 * Otherwise, the relaxation might be thwarted by some of
1268 * these constraints.
1270 * Update the underlying set to ensure that the dimension doesn't change.
1271 * Otherwise the integer divisions could get dropped if the tab
1272 * turns out to be empty.
1274 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1275 struct isl_tab *tab)
1277 isl_basic_set *bset;
1279 bmap = isl_basic_map_copy(bmap);
1280 bset = isl_basic_map_underlying_set(bmap);
1281 bset = isl_basic_set_cow(bset);
1282 bset = isl_basic_set_update_from_tab(bset, tab);
1283 return isl_set_from_basic_set(bset);
1286 /* Wrap the constraints of info->bmap that bound the facet defined
1287 * by inequality "k" around (the opposite of) this inequality to
1288 * include "set". "bound" may be used to store the negated inequality.
1289 * Since the wrapped constraints are not guaranteed to contain the whole
1290 * of info->bmap, we check them in check_wraps.
1291 * If any of the wrapped constraints turn out to be invalid, then
1292 * check_wraps will reset wrap->n_row to zero.
1294 static int add_wraps_around_facet(struct isl_wraps *wraps,
1295 struct isl_coalesce_info *info, int k, isl_int *bound,
1296 __isl_keep isl_set *set)
1298 struct isl_tab_undo *snap;
1299 int n;
1300 unsigned total = isl_basic_map_total_dim(info->bmap);
1302 snap = isl_tab_snap(info->tab);
1304 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1305 return -1;
1306 if (isl_tab_detect_redundant(info->tab) < 0)
1307 return -1;
1309 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1311 n = wraps->mat->n_row;
1312 if (add_wraps(wraps, info, bound, set) < 0)
1313 return -1;
1315 if (isl_tab_rollback(info->tab, snap) < 0)
1316 return -1;
1317 if (check_wraps(wraps->mat, n, info->tab) < 0)
1318 return -1;
1320 return 0;
1323 /* Given a basic set i with a constraint k that is adjacent to
1324 * basic set j, check if we can wrap
1325 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1326 * (always) around their ridges to include the other set.
1327 * If so, replace the pair of basic sets by their union.
1329 * All constraints of i (except k) are assumed to be valid or
1330 * cut constraints for j.
1331 * Wrapping the cut constraints to include basic map j may result
1332 * in constraints that are no longer valid of basic map i
1333 * we have to check that the resulting wrapping constraints are valid for i.
1334 * If "wrap_facet" is not set, then all constraints of i (except k)
1335 * are assumed to be valid for j.
1336 * ____ _____
1337 * / | / \
1338 * / || / |
1339 * \ || => \ |
1340 * \ || \ |
1341 * \___|| \____|
1344 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1345 struct isl_coalesce_info *info, int wrap_facet)
1347 enum isl_change change = isl_change_none;
1348 struct isl_wraps wraps;
1349 isl_ctx *ctx;
1350 isl_mat *mat;
1351 struct isl_set *set_i = NULL;
1352 struct isl_set *set_j = NULL;
1353 struct isl_vec *bound = NULL;
1354 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1356 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1357 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1358 ctx = isl_basic_map_get_ctx(info[i].bmap);
1359 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1360 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1361 1 + total);
1362 wraps_init(&wraps, mat, info, i, j);
1363 bound = isl_vec_alloc(ctx, 1 + total);
1364 if (!set_i || !set_j || !wraps.mat || !bound)
1365 goto error;
1367 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1368 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1370 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1371 wraps.mat->n_row = 1;
1373 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1374 goto error;
1375 if (!wraps.mat->n_row)
1376 goto unbounded;
1378 if (wrap_facet) {
1379 if (add_wraps_around_facet(&wraps, &info[i], k,
1380 bound->el, set_j) < 0)
1381 goto error;
1382 if (!wraps.mat->n_row)
1383 goto unbounded;
1386 change = fuse(i, j, info, wraps.mat, 0, 0);
1388 unbounded:
1389 wraps_free(&wraps);
1391 isl_set_free(set_i);
1392 isl_set_free(set_j);
1394 isl_vec_free(bound);
1396 return change;
1397 error:
1398 wraps_free(&wraps);
1399 isl_vec_free(bound);
1400 isl_set_free(set_i);
1401 isl_set_free(set_j);
1402 return isl_change_error;
1405 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1406 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1407 * add wrapping constraints to wrap.mat for all constraints
1408 * of basic map j that bound the part of basic map j that sticks out
1409 * of the cut constraint.
1410 * "set_i" is the underlying set of basic map i.
1411 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1413 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1414 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1415 * (with respect to the integer points), so we add t(x) >= 0 instead.
1416 * Otherwise, we wrap the constraints of basic map j that are not
1417 * redundant in this intersection and that are not already valid
1418 * for basic map i over basic map i.
1419 * Note that it is sufficient to wrap the constraints to include
1420 * basic map i, because we will only wrap the constraints that do
1421 * not include basic map i already. The wrapped constraint will
1422 * therefore be more relaxed compared to the original constraint.
1423 * Since the original constraint is valid for basic map j, so is
1424 * the wrapped constraint.
1426 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1427 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1428 struct isl_tab_undo *snap)
1430 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1431 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1432 return isl_stat_error;
1433 if (isl_tab_detect_redundant(info_j->tab) < 0)
1434 return isl_stat_error;
1436 if (info_j->tab->empty)
1437 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1438 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1439 return isl_stat_error;
1441 if (isl_tab_rollback(info_j->tab, snap) < 0)
1442 return isl_stat_error;
1444 return isl_stat_ok;
1447 /* Given a pair of basic maps i and j such that j sticks out
1448 * of i at n cut constraints, each time by at most one,
1449 * try to compute wrapping constraints and replace the two
1450 * basic maps by a single basic map.
1451 * The other constraints of i are assumed to be valid for j.
1452 * "set_i" is the underlying set of basic map i.
1453 * "wraps" has been initialized to be of the right size.
1455 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1456 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1457 * of basic map j that bound the part of basic map j that sticks out
1458 * of the cut constraint.
1460 * If any wrapping fails, i.e., if we cannot wrap to touch
1461 * the union, then we give up.
1462 * Otherwise, the pair of basic maps is replaced by their union.
1464 static enum isl_change try_wrap_in_facets(int i, int j,
1465 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1466 __isl_keep isl_set *set_i)
1468 int k, l, w;
1469 unsigned total;
1470 struct isl_tab_undo *snap;
1472 total = isl_basic_map_total_dim(info[i].bmap);
1474 snap = isl_tab_snap(info[j].tab);
1476 wraps->mat->n_row = 0;
1478 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1479 for (l = 0; l < 2; ++l) {
1480 if (info[i].eq[2 * k + l] != STATUS_CUT)
1481 continue;
1482 w = wraps->mat->n_row++;
1483 if (l == 0)
1484 isl_seq_neg(wraps->mat->row[w],
1485 info[i].bmap->eq[k], 1 + total);
1486 else
1487 isl_seq_cpy(wraps->mat->row[w],
1488 info[i].bmap->eq[k], 1 + total);
1489 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1490 return isl_change_error;
1492 if (!wraps->mat->n_row)
1493 return isl_change_none;
1497 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1498 if (info[i].ineq[k] != STATUS_CUT)
1499 continue;
1500 w = wraps->mat->n_row++;
1501 isl_seq_cpy(wraps->mat->row[w],
1502 info[i].bmap->ineq[k], 1 + total);
1503 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1504 return isl_change_error;
1506 if (!wraps->mat->n_row)
1507 return isl_change_none;
1510 return fuse(i, j, info, wraps->mat, 0, 1);
1513 /* Given a pair of basic maps i and j such that j sticks out
1514 * of i at n cut constraints, each time by at most one,
1515 * try to compute wrapping constraints and replace the two
1516 * basic maps by a single basic map.
1517 * The other constraints of i are assumed to be valid for j.
1519 * The core computation is performed by try_wrap_in_facets.
1520 * This function simply extracts an underlying set representation
1521 * of basic map i and initializes the data structure for keeping
1522 * track of wrapping constraints.
1524 static enum isl_change wrap_in_facets(int i, int j, int n,
1525 struct isl_coalesce_info *info)
1527 enum isl_change change = isl_change_none;
1528 struct isl_wraps wraps;
1529 isl_ctx *ctx;
1530 isl_mat *mat;
1531 isl_set *set_i = NULL;
1532 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1533 int max_wrap;
1535 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1536 return isl_change_error;
1538 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1539 max_wrap *= n;
1541 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1542 ctx = isl_basic_map_get_ctx(info[i].bmap);
1543 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1544 wraps_init(&wraps, mat, info, i, j);
1545 if (!set_i || !wraps.mat)
1546 goto error;
1548 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1550 wraps_free(&wraps);
1551 isl_set_free(set_i);
1553 return change;
1554 error:
1555 wraps_free(&wraps);
1556 isl_set_free(set_i);
1557 return isl_change_error;
1560 /* Return the effect of inequality "ineq" on the tableau "tab",
1561 * after relaxing the constant term of "ineq" by one.
1563 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1565 enum isl_ineq_type type;
1567 isl_int_add_ui(ineq[0], ineq[0], 1);
1568 type = isl_tab_ineq_type(tab, ineq);
1569 isl_int_sub_ui(ineq[0], ineq[0], 1);
1571 return type;
1574 /* Given two basic sets i and j,
1575 * check if relaxing all the cut constraints of i by one turns
1576 * them into valid constraint for j and check if we can wrap in
1577 * the bits that are sticking out.
1578 * If so, replace the pair by their union.
1580 * We first check if all relaxed cut inequalities of i are valid for j
1581 * and then try to wrap in the intersections of the relaxed cut inequalities
1582 * with j.
1584 * During this wrapping, we consider the points of j that lie at a distance
1585 * of exactly 1 from i. In particular, we ignore the points that lie in
1586 * between this lower-dimensional space and the basic map i.
1587 * We can therefore only apply this to integer maps.
1588 * ____ _____
1589 * / ___|_ / \
1590 * / | | / |
1591 * \ | | => \ |
1592 * \|____| \ |
1593 * \___| \____/
1595 * _____ ______
1596 * | ____|_ | \
1597 * | | | | |
1598 * | | | => | |
1599 * |_| | | |
1600 * |_____| \______|
1602 * _______
1603 * | |
1604 * | |\ |
1605 * | | \ |
1606 * | | \ |
1607 * | | \|
1608 * | | \
1609 * | |_____\
1610 * | |
1611 * |_______|
1613 * Wrapping can fail if the result of wrapping one of the facets
1614 * around its edges does not produce any new facet constraint.
1615 * In particular, this happens when we try to wrap in unbounded sets.
1617 * _______________________________________________________________________
1619 * | ___
1620 * | | |
1621 * |_| |_________________________________________________________________
1622 * |___|
1624 * The following is not an acceptable result of coalescing the above two
1625 * sets as it includes extra integer points.
1626 * _______________________________________________________________________
1628 * |
1629 * |
1631 * \______________________________________________________________________
1633 static enum isl_change can_wrap_in_set(int i, int j,
1634 struct isl_coalesce_info *info)
1636 int k, l;
1637 int n;
1638 unsigned total;
1640 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1641 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1642 return isl_change_none;
1644 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1645 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1646 if (n == 0)
1647 return isl_change_none;
1649 total = isl_basic_map_total_dim(info[i].bmap);
1650 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1651 for (l = 0; l < 2; ++l) {
1652 enum isl_ineq_type type;
1654 if (info[i].eq[2 * k + l] != STATUS_CUT)
1655 continue;
1657 if (l == 0)
1658 isl_seq_neg(info[i].bmap->eq[k],
1659 info[i].bmap->eq[k], 1 + total);
1660 type = type_of_relaxed(info[j].tab,
1661 info[i].bmap->eq[k]);
1662 if (l == 0)
1663 isl_seq_neg(info[i].bmap->eq[k],
1664 info[i].bmap->eq[k], 1 + total);
1665 if (type == isl_ineq_error)
1666 return isl_change_error;
1667 if (type != isl_ineq_redundant)
1668 return isl_change_none;
1672 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1673 enum isl_ineq_type type;
1675 if (info[i].ineq[k] != STATUS_CUT)
1676 continue;
1678 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1679 if (type == isl_ineq_error)
1680 return isl_change_error;
1681 if (type != isl_ineq_redundant)
1682 return isl_change_none;
1685 return wrap_in_facets(i, j, n, info);
1688 /* Check if either i or j has only cut constraints that can
1689 * be used to wrap in (a facet of) the other basic set.
1690 * if so, replace the pair by their union.
1692 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1694 enum isl_change change = isl_change_none;
1696 change = can_wrap_in_set(i, j, info);
1697 if (change != isl_change_none)
1698 return change;
1700 change = can_wrap_in_set(j, i, info);
1701 return change;
1704 /* At least one of the basic maps has an equality that is adjacent
1705 * to inequality. Make sure that only one of the basic maps has
1706 * such an equality and that the other basic map has exactly one
1707 * inequality adjacent to an equality.
1708 * If the other basic map does not have such an inequality, then
1709 * check if all its constraints are either valid or cut constraints
1710 * and, if so, try wrapping in the first map into the second.
1712 * We call the basic map that has the inequality "i" and the basic
1713 * map that has the equality "j".
1714 * If "i" has any "cut" (in)equality, then relaxing the inequality
1715 * by one would not result in a basic map that contains the other
1716 * basic map. However, it may still be possible to wrap in the other
1717 * basic map.
1719 static enum isl_change check_adj_eq(int i, int j,
1720 struct isl_coalesce_info *info)
1722 enum isl_change change = isl_change_none;
1723 int k;
1724 int any_cut;
1726 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1727 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1728 /* ADJ EQ TOO MANY */
1729 return isl_change_none;
1731 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1732 return check_adj_eq(j, i, info);
1734 /* j has an equality adjacent to an inequality in i */
1736 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1737 if (all_valid_or_cut(&info[i]))
1738 return can_wrap_in_set(i, j, info);
1739 return isl_change_none;
1741 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1742 return isl_change_none;
1743 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1744 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1745 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1746 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1747 /* ADJ EQ TOO MANY */
1748 return isl_change_none;
1750 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ);
1752 if (!any_cut) {
1753 change = is_adj_eq_extension(i, j, k, info);
1754 if (change != isl_change_none)
1755 return change;
1758 change = can_wrap_in_facet(i, j, k, info, any_cut);
1760 return change;
1763 /* The two basic maps lie on adjacent hyperplanes. In particular,
1764 * basic map "i" has an equality that lies parallel to basic map "j".
1765 * Check if we can wrap the facets around the parallel hyperplanes
1766 * to include the other set.
1768 * We perform basically the same operations as can_wrap_in_facet,
1769 * except that we don't need to select a facet of one of the sets.
1771 * \\ \\
1772 * \\ => \\
1773 * \ \|
1775 * If there is more than one equality of "i" adjacent to an equality of "j",
1776 * then the result will satisfy one or more equalities that are a linear
1777 * combination of these equalities. These will be encoded as pairs
1778 * of inequalities in the wrapping constraints and need to be made
1779 * explicit.
1781 static enum isl_change check_eq_adj_eq(int i, int j,
1782 struct isl_coalesce_info *info)
1784 int k;
1785 enum isl_change change = isl_change_none;
1786 int detect_equalities = 0;
1787 struct isl_wraps wraps;
1788 isl_ctx *ctx;
1789 isl_mat *mat;
1790 struct isl_set *set_i = NULL;
1791 struct isl_set *set_j = NULL;
1792 struct isl_vec *bound = NULL;
1793 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1795 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1796 detect_equalities = 1;
1798 k = find(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ);
1800 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1801 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1802 ctx = isl_basic_map_get_ctx(info[i].bmap);
1803 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1804 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1805 1 + total);
1806 wraps_init(&wraps, mat, info, i, j);
1807 bound = isl_vec_alloc(ctx, 1 + total);
1808 if (!set_i || !set_j || !wraps.mat || !bound)
1809 goto error;
1811 if (k % 2 == 0)
1812 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1813 else
1814 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1815 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1817 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1818 wraps.mat->n_row = 1;
1820 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1821 goto error;
1822 if (!wraps.mat->n_row)
1823 goto unbounded;
1825 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1826 isl_seq_neg(bound->el, bound->el, 1 + total);
1828 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1829 wraps.mat->n_row++;
1831 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1832 goto error;
1833 if (!wraps.mat->n_row)
1834 goto unbounded;
1836 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1838 if (0) {
1839 error: change = isl_change_error;
1841 unbounded:
1843 wraps_free(&wraps);
1844 isl_set_free(set_i);
1845 isl_set_free(set_j);
1846 isl_vec_free(bound);
1848 return change;
1851 /* Initialize the "eq" and "ineq" fields of "info".
1853 static void init_status(struct isl_coalesce_info *info)
1855 info->eq = info->ineq = NULL;
1858 /* Set info->eq to the positions of the equalities of info->bmap
1859 * with respect to the basic map represented by "tab".
1860 * If info->eq has already been computed, then do not compute it again.
1862 static void set_eq_status_in(struct isl_coalesce_info *info,
1863 struct isl_tab *tab)
1865 if (info->eq)
1866 return;
1867 info->eq = eq_status_in(info->bmap, tab);
1870 /* Set info->ineq to the positions of the inequalities of info->bmap
1871 * with respect to the basic map represented by "tab".
1872 * If info->ineq has already been computed, then do not compute it again.
1874 static void set_ineq_status_in(struct isl_coalesce_info *info,
1875 struct isl_tab *tab)
1877 if (info->ineq)
1878 return;
1879 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1882 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1883 * This function assumes that init_status has been called on "info" first,
1884 * after which the "eq" and "ineq" fields may or may not have been
1885 * assigned a newly allocated array.
1887 static void clear_status(struct isl_coalesce_info *info)
1889 free(info->eq);
1890 free(info->ineq);
1893 /* Check if the union of the given pair of basic maps
1894 * can be represented by a single basic map.
1895 * If so, replace the pair by the single basic map and return
1896 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1897 * Otherwise, return isl_change_none.
1898 * The two basic maps are assumed to live in the same local space.
1899 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1900 * to have been initialized by the caller, either to NULL or
1901 * to valid information.
1903 * We first check the effect of each constraint of one basic map
1904 * on the other basic map.
1905 * The constraint may be
1906 * redundant the constraint is redundant in its own
1907 * basic map and should be ignore and removed
1908 * in the end
1909 * valid all (integer) points of the other basic map
1910 * satisfy the constraint
1911 * separate no (integer) point of the other basic map
1912 * satisfies the constraint
1913 * cut some but not all points of the other basic map
1914 * satisfy the constraint
1915 * adj_eq the given constraint is adjacent (on the outside)
1916 * to an equality of the other basic map
1917 * adj_ineq the given constraint is adjacent (on the outside)
1918 * to an inequality of the other basic map
1920 * We consider seven cases in which we can replace the pair by a single
1921 * basic map. We ignore all "redundant" constraints.
1923 * 1. all constraints of one basic map are valid
1924 * => the other basic map is a subset and can be removed
1926 * 2. all constraints of both basic maps are either "valid" or "cut"
1927 * and the facets corresponding to the "cut" constraints
1928 * of one of the basic maps lies entirely inside the other basic map
1929 * => the pair can be replaced by a basic map consisting
1930 * of the valid constraints in both basic maps
1932 * 3. there is a single pair of adjacent inequalities
1933 * (all other constraints are "valid")
1934 * => the pair can be replaced by a basic map consisting
1935 * of the valid constraints in both basic maps
1937 * 4. one basic map has a single adjacent inequality, while the other
1938 * constraints are "valid". The other basic map has some
1939 * "cut" constraints, but replacing the adjacent inequality by
1940 * its opposite and adding the valid constraints of the other
1941 * basic map results in a subset of the other basic map
1942 * => the pair can be replaced by a basic map consisting
1943 * of the valid constraints in both basic maps
1945 * 5. there is a single adjacent pair of an inequality and an equality,
1946 * the other constraints of the basic map containing the inequality are
1947 * "valid". Moreover, if the inequality the basic map is relaxed
1948 * and then turned into an equality, then resulting facet lies
1949 * entirely inside the other basic map
1950 * => the pair can be replaced by the basic map containing
1951 * the inequality, with the inequality relaxed.
1953 * 6. there is a single adjacent pair of an inequality and an equality,
1954 * the other constraints of the basic map containing the inequality are
1955 * "valid". Moreover, the facets corresponding to both
1956 * the inequality and the equality can be wrapped around their
1957 * ridges to include the other basic map
1958 * => the pair can be replaced by a basic map consisting
1959 * of the valid constraints in both basic maps together
1960 * with all wrapping constraints
1962 * 7. one of the basic maps extends beyond the other by at most one.
1963 * Moreover, the facets corresponding to the cut constraints and
1964 * the pieces of the other basic map at offset one from these cut
1965 * constraints can be wrapped around their ridges to include
1966 * the union of the two basic maps
1967 * => the pair can be replaced by a basic map consisting
1968 * of the valid constraints in both basic maps together
1969 * with all wrapping constraints
1971 * 8. the two basic maps live in adjacent hyperplanes. In principle
1972 * such sets can always be combined through wrapping, but we impose
1973 * that there is only one such pair, to avoid overeager coalescing.
1975 * Throughout the computation, we maintain a collection of tableaus
1976 * corresponding to the basic maps. When the basic maps are dropped
1977 * or combined, the tableaus are modified accordingly.
1979 static enum isl_change coalesce_local_pair_reuse(int i, int j,
1980 struct isl_coalesce_info *info)
1982 enum isl_change change = isl_change_none;
1984 set_eq_status_in(&info[i], info[j].tab);
1985 if (info[i].bmap->n_eq && !info[i].eq)
1986 goto error;
1987 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1988 goto error;
1989 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1990 goto done;
1992 set_eq_status_in(&info[j], info[i].tab);
1993 if (info[j].bmap->n_eq && !info[j].eq)
1994 goto error;
1995 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1996 goto error;
1997 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1998 goto done;
2000 set_ineq_status_in(&info[i], info[j].tab);
2001 if (info[i].bmap->n_ineq && !info[i].ineq)
2002 goto error;
2003 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
2004 goto error;
2005 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
2006 goto done;
2008 set_ineq_status_in(&info[j], info[i].tab);
2009 if (info[j].bmap->n_ineq && !info[j].ineq)
2010 goto error;
2011 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2012 goto error;
2013 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2014 goto done;
2016 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2017 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2018 drop(&info[j]);
2019 change = isl_change_drop_second;
2020 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2021 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2022 drop(&info[i]);
2023 change = isl_change_drop_first;
2024 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2025 change = check_eq_adj_eq(i, j, info);
2026 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2027 change = check_eq_adj_eq(j, i, info);
2028 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2029 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2030 change = check_adj_eq(i, j, info);
2031 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2032 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2033 /* Can't happen */
2034 /* BAD ADJ INEQ */
2035 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2036 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2037 change = check_adj_ineq(i, j, info);
2038 } else {
2039 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2040 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2041 change = check_facets(i, j, info);
2042 if (change == isl_change_none)
2043 change = check_wrap(i, j, info);
2046 done:
2047 clear_status(&info[i]);
2048 clear_status(&info[j]);
2049 return change;
2050 error:
2051 clear_status(&info[i]);
2052 clear_status(&info[j]);
2053 return isl_change_error;
2056 /* Check if the union of the given pair of basic maps
2057 * can be represented by a single basic map.
2058 * If so, replace the pair by the single basic map and return
2059 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2060 * Otherwise, return isl_change_none.
2061 * The two basic maps are assumed to live in the same local space.
2063 static enum isl_change coalesce_local_pair(int i, int j,
2064 struct isl_coalesce_info *info)
2066 init_status(&info[i]);
2067 init_status(&info[j]);
2068 return coalesce_local_pair_reuse(i, j, info);
2071 /* Shift the integer division at position "div" of the basic map
2072 * represented by "info" by "shift".
2074 * That is, if the integer division has the form
2076 * floor(f(x)/d)
2078 * then replace it by
2080 * floor((f(x) + shift * d)/d) - shift
2082 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
2084 unsigned total;
2086 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2087 if (!info->bmap)
2088 return -1;
2090 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2091 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2092 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2093 return -1;
2095 return 0;
2098 /* Check if some of the divs in the basic map represented by "info1"
2099 * are shifts of the corresponding divs in the basic map represented
2100 * by "info2". If so, align them with those of "info2".
2101 * Only do this if "info1" and "info2" have the same number
2102 * of integer divisions.
2104 * An integer division is considered to be a shift of another integer
2105 * division if one is equal to the other plus a constant.
2107 * In particular, for each pair of integer divisions, if both are known,
2108 * have identical coefficients (apart from the constant term) and
2109 * if the difference between the constant terms (taking into account
2110 * the denominator) is an integer, then move the difference outside.
2111 * That is, if one integer division is of the form
2113 * floor((f(x) + c_1)/d)
2115 * while the other is of the form
2117 * floor((f(x) + c_2)/d)
2119 * and n = (c_2 - c_1)/d is an integer, then replace the first
2120 * integer division by
2122 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2124 static int harmonize_divs(struct isl_coalesce_info *info1,
2125 struct isl_coalesce_info *info2)
2127 int i;
2128 int total;
2130 if (!info1->bmap || !info2->bmap)
2131 return -1;
2133 if (info1->bmap->n_div != info2->bmap->n_div)
2134 return 0;
2135 if (info1->bmap->n_div == 0)
2136 return 0;
2138 total = isl_basic_map_total_dim(info1->bmap);
2139 for (i = 0; i < info1->bmap->n_div; ++i) {
2140 isl_int d;
2141 int r = 0;
2143 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
2144 isl_int_is_zero(info2->bmap->div[i][0]))
2145 continue;
2146 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2147 continue;
2148 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
2149 continue;
2150 if (!isl_seq_eq(info1->bmap->div[i] + 2,
2151 info2->bmap->div[i] + 2, total))
2152 continue;
2153 isl_int_init(d);
2154 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
2155 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
2156 isl_int_divexact(d, d, info1->bmap->div[i][0]);
2157 r = shift_div(info1, i, d);
2159 isl_int_clear(d);
2160 if (r < 0)
2161 return -1;
2164 return 0;
2167 /* Do the two basic maps live in the same local space, i.e.,
2168 * do they have the same (known) divs?
2169 * If either basic map has any unknown divs, then we can only assume
2170 * that they do not live in the same local space.
2172 static int same_divs(__isl_keep isl_basic_map *bmap1,
2173 __isl_keep isl_basic_map *bmap2)
2175 int i;
2176 int known;
2177 int total;
2179 if (!bmap1 || !bmap2)
2180 return -1;
2181 if (bmap1->n_div != bmap2->n_div)
2182 return 0;
2184 if (bmap1->n_div == 0)
2185 return 1;
2187 known = isl_basic_map_divs_known(bmap1);
2188 if (known < 0 || !known)
2189 return known;
2190 known = isl_basic_map_divs_known(bmap2);
2191 if (known < 0 || !known)
2192 return known;
2194 total = isl_basic_map_total_dim(bmap1);
2195 for (i = 0; i < bmap1->n_div; ++i)
2196 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2197 return 0;
2199 return 1;
2202 /* Expand info->tab in the same way info->bmap was expanded in
2203 * isl_basic_map_expand_divs using the expansion "exp" and
2204 * update info->ineq with respect to the redundant constraints
2205 * in the resulting tableau. "bmap" is the original version
2206 * of info->bmap, i.e., the one that corresponds to the current
2207 * state of info->tab. The number of constraints in "bmap"
2208 * is assumed to be the same as the number of constraints
2209 * in info->tab. This is required to be able to detect
2210 * the extra constraints in info->bmap.
2212 * In particular, introduce extra variables corresponding
2213 * to the extra integer divisions and add the div constraints
2214 * that were added to info->bmap after info->tab was created
2215 * from the original info->bmap.
2216 * info->ineq was computed without a tableau and therefore
2217 * does not take into account the redundant constraints
2218 * in the tableau. Mark them here.
2220 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2221 __isl_keep isl_basic_map *bmap)
2223 unsigned total, pos, n_div;
2224 int extra_var;
2225 int i, n, j, n_ineq;
2226 unsigned n_eq;
2228 if (!bmap)
2229 return isl_stat_error;
2230 if (bmap->n_eq + bmap->n_ineq != info->tab->n_con)
2231 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2232 "original tableau does not correspond "
2233 "to original basic map", return isl_stat_error);
2235 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2236 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2237 pos = total - n_div;
2238 extra_var = total - info->tab->n_var;
2239 n = n_div - extra_var;
2241 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2242 return isl_stat_error;
2243 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2244 return isl_stat_error;
2246 i = 0;
2247 for (j = 0; j < n_div; ++j) {
2248 if (i < n && exp[i] == j) {
2249 ++i;
2250 continue;
2252 if (isl_tab_insert_var(info->tab, pos + j) < 0)
2253 return isl_stat_error;
2256 n_ineq = info->tab->n_con - info->tab->n_eq;
2257 for (i = n_ineq; i < info->bmap->n_ineq; ++i)
2258 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2259 return isl_stat_error;
2261 n_eq = info->bmap->n_eq;
2262 for (i = 0; i < info->bmap->n_ineq; ++i) {
2263 if (isl_tab_is_redundant(info->tab, n_eq + i))
2264 info->ineq[i] = STATUS_REDUNDANT;
2267 return isl_stat_ok;
2270 /* Check if the union of the basic maps represented by info[i] and info[j]
2271 * can be represented by a single basic map,
2272 * after expanding the divs of info[i] to match those of info[j].
2273 * If so, replace the pair by the single basic map and return
2274 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2275 * Otherwise, return isl_change_none.
2277 * The caller has already checked for info[j] being a subset of info[i].
2278 * If some of the divs of info[j] are unknown, then the expanded info[i]
2279 * will not have the corresponding div constraints. The other patterns
2280 * therefore cannot apply. Skip the computation in this case.
2282 * The expansion is performed using the divs "div" and expansion "exp"
2283 * computed by the caller.
2284 * info[i].bmap has already been expanded and the result is passed in
2285 * as "bmap".
2286 * The "eq" and "ineq" fields of info[i] reflect the status of
2287 * the constraints of the expanded "bmap" with respect to info[j].tab.
2288 * However, inequality constraints that are redundant in info[i].tab
2289 * have not yet been marked as such because no tableau was available.
2291 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2292 * updating info[i].ineq with respect to the redundant constraints.
2293 * Then try and coalesce the expanded info[i] with info[j],
2294 * reusing the information in info[i].eq and info[i].ineq.
2295 * If this does not result in any coalescing or if it results in info[j]
2296 * getting dropped (which should not happen in practice, since the case
2297 * of info[j] being a subset of info[i] has already been checked by
2298 * the caller), then revert info[i] to its original state.
2300 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2301 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2302 int *exp)
2304 isl_bool known;
2305 isl_basic_map *bmap_i;
2306 struct isl_tab_undo *snap;
2307 enum isl_change change = isl_change_none;
2309 known = isl_basic_map_divs_known(info[j].bmap);
2310 if (known < 0 || !known) {
2311 clear_status(&info[i]);
2312 isl_basic_map_free(bmap);
2313 return known < 0 ? isl_change_error : isl_change_none;
2316 bmap_i = info[i].bmap;
2317 info[i].bmap = isl_basic_map_copy(bmap);
2318 snap = isl_tab_snap(info[i].tab);
2319 if (!info[i].bmap || expand_tab(&info[i], exp, bmap_i) < 0)
2320 change = isl_change_error;
2322 init_status(&info[j]);
2323 if (change == isl_change_none)
2324 change = coalesce_local_pair_reuse(i, j, info);
2325 else
2326 clear_status(&info[i]);
2327 if (change != isl_change_none && change != isl_change_drop_second) {
2328 isl_basic_map_free(bmap_i);
2329 } else {
2330 isl_basic_map_free(info[i].bmap);
2331 info[i].bmap = bmap_i;
2333 if (isl_tab_rollback(info[i].tab, snap) < 0)
2334 change = isl_change_error;
2337 isl_basic_map_free(bmap);
2338 return change;
2341 /* Check if the union of "bmap" and the basic map represented by info[j]
2342 * can be represented by a single basic map,
2343 * after expanding the divs of "bmap" to match those of info[j].
2344 * If so, replace the pair by the single basic map and return
2345 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2346 * Otherwise, return isl_change_none.
2348 * In particular, check if the expanded "bmap" contains the basic map
2349 * represented by the tableau info[j].tab.
2350 * The expansion is performed using the divs "div" and expansion "exp"
2351 * computed by the caller.
2352 * Then we check if all constraints of the expanded "bmap" are valid for
2353 * info[j].tab.
2355 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2356 * In this case, the positions of the constraints of info[i].bmap
2357 * with respect to the basic map represented by info[j] are stored
2358 * in info[i].
2360 * If the expanded "bmap" does not contain the basic map
2361 * represented by the tableau info[j].tab and if "i" is not -1,
2362 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2363 * as well and check if that results in coalescing.
2365 static enum isl_change coalesce_with_expanded_divs(
2366 __isl_keep isl_basic_map *bmap, int i, int j,
2367 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2369 enum isl_change change = isl_change_none;
2370 struct isl_coalesce_info info_local, *info_i;
2372 info_i = i >= 0 ? &info[i] : &info_local;
2373 init_status(info_i);
2374 bmap = isl_basic_map_copy(bmap);
2375 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2376 bmap = isl_basic_map_mark_final(bmap);
2378 if (!bmap)
2379 goto error;
2381 info_i->eq = eq_status_in(bmap, info[j].tab);
2382 if (bmap->n_eq && !info_i->eq)
2383 goto error;
2384 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2385 goto error;
2386 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2387 goto done;
2389 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2390 if (bmap->n_ineq && !info_i->ineq)
2391 goto error;
2392 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2393 goto error;
2394 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2395 goto done;
2397 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2398 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2399 drop(&info[j]);
2400 change = isl_change_drop_second;
2403 if (change == isl_change_none && i != -1)
2404 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2406 done:
2407 isl_basic_map_free(bmap);
2408 clear_status(info_i);
2409 return change;
2410 error:
2411 isl_basic_map_free(bmap);
2412 clear_status(info_i);
2413 return isl_change_error;
2416 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2417 * can be represented by a single basic map,
2418 * after aligning the divs of "bmap_i" to match those of info[j].
2419 * If so, replace the pair by the single basic map and return
2420 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2421 * Otherwise, return isl_change_none.
2423 * In particular, check if "bmap_i" contains the basic map represented by
2424 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2425 * Note that this can only succeed if the number of divs of "bmap_i"
2426 * is smaller than (or equal to) the number of divs of info[j].
2428 * We first check if the divs of "bmap_i" are all known and form a subset
2429 * of those of info[j].bmap. If so, we pass control over to
2430 * coalesce_with_expanded_divs.
2432 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2434 static enum isl_change coalesce_after_aligning_divs(
2435 __isl_keep isl_basic_map *bmap_i, int i, int j,
2436 struct isl_coalesce_info *info)
2438 int known;
2439 isl_mat *div_i, *div_j, *div;
2440 int *exp1 = NULL;
2441 int *exp2 = NULL;
2442 isl_ctx *ctx;
2443 enum isl_change change;
2445 known = isl_basic_map_divs_known(bmap_i);
2446 if (known < 0 || !known)
2447 return known;
2449 ctx = isl_basic_map_get_ctx(bmap_i);
2451 div_i = isl_basic_map_get_divs(bmap_i);
2452 div_j = isl_basic_map_get_divs(info[j].bmap);
2454 if (!div_i || !div_j)
2455 goto error;
2457 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2458 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2459 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2460 goto error;
2462 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2463 if (!div)
2464 goto error;
2466 if (div->n_row == div_j->n_row)
2467 change = coalesce_with_expanded_divs(bmap_i,
2468 i, j, info, div, exp1);
2469 else
2470 change = isl_change_none;
2472 isl_mat_free(div);
2474 isl_mat_free(div_i);
2475 isl_mat_free(div_j);
2477 free(exp2);
2478 free(exp1);
2480 return change;
2481 error:
2482 isl_mat_free(div_i);
2483 isl_mat_free(div_j);
2484 free(exp1);
2485 free(exp2);
2486 return isl_change_error;
2489 /* Check if basic map "j" is a subset of basic map "i" after
2490 * exploiting the extra equalities of "j" to simplify the divs of "i".
2491 * If so, remove basic map "j" and return isl_change_drop_second.
2493 * If "j" does not have any equalities or if they are the same
2494 * as those of "i", then we cannot exploit them to simplify the divs.
2495 * Similarly, if there are no divs in "i", then they cannot be simplified.
2496 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2497 * then "j" cannot be a subset of "i".
2499 * Otherwise, we intersect "i" with the affine hull of "j" and then
2500 * check if "j" is a subset of the result after aligning the divs.
2501 * If so, then "j" is definitely a subset of "i" and can be removed.
2502 * Note that if after intersection with the affine hull of "j".
2503 * "i" still has more divs than "j", then there is no way we can
2504 * align the divs of "i" to those of "j".
2506 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2507 struct isl_coalesce_info *info)
2509 isl_basic_map *hull_i, *hull_j, *bmap_i;
2510 int equal, empty;
2511 enum isl_change change;
2513 if (info[j].bmap->n_eq == 0)
2514 return isl_change_none;
2515 if (info[i].bmap->n_div == 0)
2516 return isl_change_none;
2518 hull_i = isl_basic_map_copy(info[i].bmap);
2519 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2520 hull_j = isl_basic_map_copy(info[j].bmap);
2521 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2523 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2524 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2525 empty = isl_basic_map_plain_is_empty(hull_j);
2526 isl_basic_map_free(hull_i);
2528 if (equal < 0 || equal || empty < 0 || empty) {
2529 isl_basic_map_free(hull_j);
2530 if (equal < 0 || empty < 0)
2531 return isl_change_error;
2532 return isl_change_none;
2535 bmap_i = isl_basic_map_copy(info[i].bmap);
2536 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2537 if (!bmap_i)
2538 return isl_change_error;
2540 if (bmap_i->n_div > info[j].bmap->n_div) {
2541 isl_basic_map_free(bmap_i);
2542 return isl_change_none;
2545 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2547 isl_basic_map_free(bmap_i);
2549 return change;
2552 /* Check if the union of and the basic maps represented by info[i] and info[j]
2553 * can be represented by a single basic map, by aligning or equating
2554 * their integer divisions.
2555 * If so, replace the pair by the single basic map and return
2556 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2557 * Otherwise, return isl_change_none.
2559 * Note that we only perform any test if the number of divs is different
2560 * in the two basic maps. In case the number of divs is the same,
2561 * we have already established that the divs are different
2562 * in the two basic maps.
2563 * In particular, if the number of divs of basic map i is smaller than
2564 * the number of divs of basic map j, then we check if j is a subset of i
2565 * and vice versa.
2567 static enum isl_change coalesce_divs(int i, int j,
2568 struct isl_coalesce_info *info)
2570 enum isl_change change = isl_change_none;
2572 if (info[i].bmap->n_div < info[j].bmap->n_div)
2573 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2574 if (change != isl_change_none)
2575 return change;
2577 if (info[j].bmap->n_div < info[i].bmap->n_div)
2578 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2579 if (change != isl_change_none)
2580 return invert_change(change);
2582 change = coalesce_subset_with_equalities(i, j, info);
2583 if (change != isl_change_none)
2584 return change;
2586 change = coalesce_subset_with_equalities(j, i, info);
2587 if (change != isl_change_none)
2588 return invert_change(change);
2590 return isl_change_none;
2593 /* Does "bmap" involve any divs that themselves refer to divs?
2595 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2597 int i;
2598 unsigned total;
2599 unsigned n_div;
2601 total = isl_basic_map_dim(bmap, isl_dim_all);
2602 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2603 total -= n_div;
2605 for (i = 0; i < n_div; ++i)
2606 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2607 n_div) != -1)
2608 return 1;
2610 return 0;
2613 /* Return a list of affine expressions, one for each integer division
2614 * in "bmap_i". For each integer division that also appears in "bmap_j",
2615 * the affine expression is set to NaN. The number of NaNs in the list
2616 * is equal to the number of integer divisions in "bmap_j".
2617 * For the other integer divisions of "bmap_i", the corresponding
2618 * element in the list is a purely affine expression equal to the integer
2619 * division in "hull".
2620 * If no such list can be constructed, then the number of elements
2621 * in the returned list is smaller than the number of integer divisions
2622 * in "bmap_i".
2624 static __isl_give isl_aff_list *set_up_substitutions(
2625 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2626 __isl_take isl_basic_map *hull)
2628 unsigned n_div_i, n_div_j, total;
2629 isl_ctx *ctx;
2630 isl_local_space *ls;
2631 isl_basic_set *wrap_hull;
2632 isl_aff *aff_nan;
2633 isl_aff_list *list;
2634 int i, j;
2636 if (!hull)
2637 return NULL;
2639 ctx = isl_basic_map_get_ctx(hull);
2641 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2642 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2643 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2645 ls = isl_basic_map_get_local_space(bmap_i);
2646 ls = isl_local_space_wrap(ls);
2647 wrap_hull = isl_basic_map_wrap(hull);
2649 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2650 list = isl_aff_list_alloc(ctx, n_div_i);
2652 j = 0;
2653 for (i = 0; i < n_div_i; ++i) {
2654 isl_aff *aff;
2656 if (j < n_div_j &&
2657 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2658 ++j;
2659 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2660 continue;
2662 if (n_div_i - i <= n_div_j - j)
2663 break;
2665 aff = isl_local_space_get_div(ls, i);
2666 aff = isl_aff_substitute_equalities(aff,
2667 isl_basic_set_copy(wrap_hull));
2668 aff = isl_aff_floor(aff);
2669 if (!aff)
2670 goto error;
2671 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2672 isl_aff_free(aff);
2673 break;
2676 list = isl_aff_list_add(list, aff);
2679 isl_aff_free(aff_nan);
2680 isl_local_space_free(ls);
2681 isl_basic_set_free(wrap_hull);
2683 return list;
2684 error:
2685 isl_aff_free(aff_nan);
2686 isl_local_space_free(ls);
2687 isl_basic_set_free(wrap_hull);
2688 isl_aff_list_free(list);
2689 return NULL;
2692 /* Add variables to info->bmap and info->tab corresponding to the elements
2693 * in "list" that are not set to NaN.
2694 * "extra_var" is the number of these elements.
2695 * "dim" is the offset in the variables of "tab" where we should
2696 * start considering the elements in "list".
2697 * When this function returns, the total number of variables in "tab"
2698 * is equal to "dim" plus the number of elements in "list".
2700 * The newly added existentially quantified variables are not given
2701 * an explicit representation because the corresponding div constraints
2702 * do not appear in info->bmap. These constraints are not added
2703 * to info->bmap because for internal consistency, they would need to
2704 * be added to info->tab as well, where they could combine with the equality
2705 * that is added later to result in constraints that do not hold
2706 * in the original input.
2708 static int add_sub_vars(struct isl_coalesce_info *info,
2709 __isl_keep isl_aff_list *list, int dim, int extra_var)
2711 int i, j, n, d;
2712 isl_space *space;
2714 space = isl_basic_map_get_space(info->bmap);
2715 info->bmap = isl_basic_map_cow(info->bmap);
2716 info->bmap = isl_basic_map_extend_space(info->bmap, space,
2717 extra_var, 0, 0);
2718 if (!info->bmap)
2719 return -1;
2720 n = isl_aff_list_n_aff(list);
2721 for (i = 0; i < n; ++i) {
2722 int is_nan;
2723 isl_aff *aff;
2725 aff = isl_aff_list_get_aff(list, i);
2726 is_nan = isl_aff_is_nan(aff);
2727 isl_aff_free(aff);
2728 if (is_nan < 0)
2729 return -1;
2730 if (is_nan)
2731 continue;
2733 if (isl_tab_insert_var(info->tab, dim + i) < 0)
2734 return -1;
2735 d = isl_basic_map_alloc_div(info->bmap);
2736 if (d < 0)
2737 return -1;
2738 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
2739 if (!info->bmap)
2740 return -1;
2741 for (j = d; j > i; --j)
2742 isl_basic_map_swap_div(info->bmap, j - 1, j);
2745 return 0;
2748 /* For each element in "list" that is not set to NaN, fix the corresponding
2749 * variable in "tab" to the purely affine expression defined by the element.
2750 * "dim" is the offset in the variables of "tab" where we should
2751 * start considering the elements in "list".
2753 * This function assumes that a sufficient number of rows and
2754 * elements in the constraint array are available in the tableau.
2756 static int add_sub_equalities(struct isl_tab *tab,
2757 __isl_keep isl_aff_list *list, int dim)
2759 int i, n;
2760 isl_ctx *ctx;
2761 isl_vec *sub;
2762 isl_aff *aff;
2764 n = isl_aff_list_n_aff(list);
2766 ctx = isl_tab_get_ctx(tab);
2767 sub = isl_vec_alloc(ctx, 1 + dim + n);
2768 if (!sub)
2769 return -1;
2770 isl_seq_clr(sub->el + 1 + dim, n);
2772 for (i = 0; i < n; ++i) {
2773 aff = isl_aff_list_get_aff(list, i);
2774 if (!aff)
2775 goto error;
2776 if (isl_aff_is_nan(aff)) {
2777 isl_aff_free(aff);
2778 continue;
2780 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2781 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2782 if (isl_tab_add_eq(tab, sub->el) < 0)
2783 goto error;
2784 isl_int_set_si(sub->el[1 + dim + i], 0);
2785 isl_aff_free(aff);
2788 isl_vec_free(sub);
2789 return 0;
2790 error:
2791 isl_aff_free(aff);
2792 isl_vec_free(sub);
2793 return -1;
2796 /* Add variables to info->tab and info->bmap corresponding to the elements
2797 * in "list" that are not set to NaN. The value of the added variable
2798 * in info->tab is fixed to the purely affine expression defined by the element.
2799 * "dim" is the offset in the variables of info->tab where we should
2800 * start considering the elements in "list".
2801 * When this function returns, the total number of variables in info->tab
2802 * is equal to "dim" plus the number of elements in "list".
2804 static int add_subs(struct isl_coalesce_info *info,
2805 __isl_keep isl_aff_list *list, int dim)
2807 int extra_var;
2808 int n;
2810 if (!list)
2811 return -1;
2813 n = isl_aff_list_n_aff(list);
2814 extra_var = n - (info->tab->n_var - dim);
2816 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2817 return -1;
2818 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2819 return -1;
2820 if (add_sub_vars(info, list, dim, extra_var) < 0)
2821 return -1;
2823 return add_sub_equalities(info->tab, list, dim);
2826 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2827 * divisions in "i" but not in "j" to basic map "j", with values
2828 * specified by "list". The total number of elements in "list"
2829 * is equal to the number of integer divisions in "i", while the number
2830 * of NaN elements in the list is equal to the number of integer divisions
2831 * in "j".
2833 * If no coalescing can be performed, then we need to revert basic map "j"
2834 * to its original state. We do the same if basic map "i" gets dropped
2835 * during the coalescing, even though this should not happen in practice
2836 * since we have already checked for "j" being a subset of "i"
2837 * before we reach this stage.
2839 static enum isl_change coalesce_with_subs(int i, int j,
2840 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2842 isl_basic_map *bmap_j;
2843 struct isl_tab_undo *snap;
2844 unsigned dim;
2845 enum isl_change change;
2847 bmap_j = isl_basic_map_copy(info[j].bmap);
2848 snap = isl_tab_snap(info[j].tab);
2850 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2851 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2852 if (add_subs(&info[j], list, dim) < 0)
2853 goto error;
2855 change = coalesce_local_pair(i, j, info);
2856 if (change != isl_change_none && change != isl_change_drop_first) {
2857 isl_basic_map_free(bmap_j);
2858 } else {
2859 isl_basic_map_free(info[j].bmap);
2860 info[j].bmap = bmap_j;
2862 if (isl_tab_rollback(info[j].tab, snap) < 0)
2863 return isl_change_error;
2866 return change;
2867 error:
2868 isl_basic_map_free(bmap_j);
2869 return isl_change_error;
2872 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2873 * those extra integer divisions in "i" that can be simplified away
2874 * using the extra equalities in "j".
2875 * All divs are assumed to be known and not contain any nested divs.
2877 * We first check if there are any extra equalities in "j" that we
2878 * can exploit. Then we check if every integer division in "i"
2879 * either already appears in "j" or can be simplified using the
2880 * extra equalities to a purely affine expression.
2881 * If these tests succeed, then we try to coalesce the two basic maps
2882 * by introducing extra dimensions in "j" corresponding to
2883 * the extra integer divsisions "i" fixed to the corresponding
2884 * purely affine expression.
2886 static enum isl_change check_coalesce_into_eq(int i, int j,
2887 struct isl_coalesce_info *info)
2889 unsigned n_div_i, n_div_j;
2890 isl_basic_map *hull_i, *hull_j;
2891 int equal, empty;
2892 isl_aff_list *list;
2893 enum isl_change change;
2895 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2896 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2897 if (n_div_i <= n_div_j)
2898 return isl_change_none;
2899 if (info[j].bmap->n_eq == 0)
2900 return isl_change_none;
2902 hull_i = isl_basic_map_copy(info[i].bmap);
2903 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2904 hull_j = isl_basic_map_copy(info[j].bmap);
2905 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2907 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2908 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2909 empty = isl_basic_map_plain_is_empty(hull_j);
2910 isl_basic_map_free(hull_i);
2912 if (equal < 0 || empty < 0)
2913 goto error;
2914 if (equal || empty) {
2915 isl_basic_map_free(hull_j);
2916 return isl_change_none;
2919 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2920 if (!list)
2921 return isl_change_error;
2922 if (isl_aff_list_n_aff(list) < n_div_i)
2923 change = isl_change_none;
2924 else
2925 change = coalesce_with_subs(i, j, info, list);
2927 isl_aff_list_free(list);
2929 return change;
2930 error:
2931 isl_basic_map_free(hull_j);
2932 return isl_change_error;
2935 /* Check if we can coalesce basic maps "i" and "j" after copying
2936 * those extra integer divisions in one of the basic maps that can
2937 * be simplified away using the extra equalities in the other basic map.
2938 * We require all divs to be known in both basic maps.
2939 * Furthermore, to simplify the comparison of div expressions,
2940 * we do not allow any nested integer divisions.
2942 static enum isl_change check_coalesce_eq(int i, int j,
2943 struct isl_coalesce_info *info)
2945 int known, nested;
2946 enum isl_change change;
2948 known = isl_basic_map_divs_known(info[i].bmap);
2949 if (known < 0 || !known)
2950 return known < 0 ? isl_change_error : isl_change_none;
2951 known = isl_basic_map_divs_known(info[j].bmap);
2952 if (known < 0 || !known)
2953 return known < 0 ? isl_change_error : isl_change_none;
2954 nested = has_nested_div(info[i].bmap);
2955 if (nested < 0 || nested)
2956 return nested < 0 ? isl_change_error : isl_change_none;
2957 nested = has_nested_div(info[j].bmap);
2958 if (nested < 0 || nested)
2959 return nested < 0 ? isl_change_error : isl_change_none;
2961 change = check_coalesce_into_eq(i, j, info);
2962 if (change != isl_change_none)
2963 return change;
2964 change = check_coalesce_into_eq(j, i, info);
2965 if (change != isl_change_none)
2966 return invert_change(change);
2968 return isl_change_none;
2971 /* Check if the union of the given pair of basic maps
2972 * can be represented by a single basic map.
2973 * If so, replace the pair by the single basic map and return
2974 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2975 * Otherwise, return isl_change_none.
2977 * We first check if the two basic maps live in the same local space,
2978 * after aligning the divs that differ by only an integer constant.
2979 * If so, we do the complete check. Otherwise, we check if they have
2980 * the same number of integer divisions and can be coalesced, if one is
2981 * an obvious subset of the other or if the extra integer divisions
2982 * of one basic map can be simplified away using the extra equalities
2983 * of the other basic map.
2985 static enum isl_change coalesce_pair(int i, int j,
2986 struct isl_coalesce_info *info)
2988 int same;
2989 enum isl_change change;
2991 if (harmonize_divs(&info[i], &info[j]) < 0)
2992 return isl_change_error;
2993 same = same_divs(info[i].bmap, info[j].bmap);
2994 if (same < 0)
2995 return isl_change_error;
2996 if (same)
2997 return coalesce_local_pair(i, j, info);
2999 if (info[i].bmap->n_div == info[j].bmap->n_div) {
3000 change = coalesce_local_pair(i, j, info);
3001 if (change != isl_change_none)
3002 return change;
3005 change = coalesce_divs(i, j, info);
3006 if (change != isl_change_none)
3007 return change;
3009 return check_coalesce_eq(i, j, info);
3012 /* Return the maximum of "a" and "b".
3014 static int isl_max(int a, int b)
3016 return a > b ? a : b;
3019 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3020 * with those in the range [start2, end2[, skipping basic maps
3021 * that have been removed (either before or within this function).
3023 * For each basic map i in the first range, we check if it can be coalesced
3024 * with respect to any previously considered basic map j in the second range.
3025 * If i gets dropped (because it was a subset of some j), then
3026 * we can move on to the next basic map.
3027 * If j gets dropped, we need to continue checking against the other
3028 * previously considered basic maps.
3029 * If the two basic maps got fused, then we recheck the fused basic map
3030 * against the previously considered basic maps, starting at i + 1
3031 * (even if start2 is greater than i + 1).
3033 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3034 int start1, int end1, int start2, int end2)
3036 int i, j;
3038 for (i = end1 - 1; i >= start1; --i) {
3039 if (info[i].removed)
3040 continue;
3041 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3042 enum isl_change changed;
3044 if (info[j].removed)
3045 continue;
3046 if (info[i].removed)
3047 isl_die(ctx, isl_error_internal,
3048 "basic map unexpectedly removed",
3049 return -1);
3050 changed = coalesce_pair(i, j, info);
3051 switch (changed) {
3052 case isl_change_error:
3053 return -1;
3054 case isl_change_none:
3055 case isl_change_drop_second:
3056 continue;
3057 case isl_change_drop_first:
3058 j = end2;
3059 break;
3060 case isl_change_fuse:
3061 j = i;
3062 break;
3067 return 0;
3070 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3072 * We consider groups of basic maps that live in the same apparent
3073 * affine hull and we first coalesce within such a group before we
3074 * coalesce the elements in the group with elements of previously
3075 * considered groups. If a fuse happens during the second phase,
3076 * then we also reconsider the elements within the group.
3078 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3080 int start, end;
3082 for (end = n; end > 0; end = start) {
3083 start = end - 1;
3084 while (start >= 1 &&
3085 info[start - 1].hull_hash == info[start].hull_hash)
3086 start--;
3087 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3088 return -1;
3089 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3090 return -1;
3093 return 0;
3096 /* Update the basic maps in "map" based on the information in "info".
3097 * In particular, remove the basic maps that have been marked removed and
3098 * update the others based on the information in the corresponding tableau.
3099 * Since we detected implicit equalities without calling
3100 * isl_basic_map_gauss, we need to do it now.
3101 * Also call isl_basic_map_simplify if we may have lost the definition
3102 * of one or more integer divisions.
3104 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3105 int n, struct isl_coalesce_info *info)
3107 int i;
3109 if (!map)
3110 return NULL;
3112 for (i = n - 1; i >= 0; --i) {
3113 if (info[i].removed) {
3114 isl_basic_map_free(map->p[i]);
3115 if (i != map->n - 1)
3116 map->p[i] = map->p[map->n - 1];
3117 map->n--;
3118 continue;
3121 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3122 info[i].tab);
3123 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3124 if (info[i].simplify)
3125 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3126 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3127 if (!info[i].bmap)
3128 return isl_map_free(map);
3129 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3130 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3131 isl_basic_map_free(map->p[i]);
3132 map->p[i] = info[i].bmap;
3133 info[i].bmap = NULL;
3136 return map;
3139 /* For each pair of basic maps in the map, check if the union of the two
3140 * can be represented by a single basic map.
3141 * If so, replace the pair by the single basic map and start over.
3143 * We factor out any (hidden) common factor from the constraint
3144 * coefficients to improve the detection of adjacent constraints.
3146 * Since we are constructing the tableaus of the basic maps anyway,
3147 * we exploit them to detect implicit equalities and redundant constraints.
3148 * This also helps the coalescing as it can ignore the redundant constraints.
3149 * In order to avoid confusion, we make all implicit equalities explicit
3150 * in the basic maps. We don't call isl_basic_map_gauss, though,
3151 * as that may affect the number of constraints.
3152 * This means that we have to call isl_basic_map_gauss at the end
3153 * of the computation (in update_basic_maps) to ensure that
3154 * the basic maps are not left in an unexpected state.
3155 * For each basic map, we also compute the hash of the apparent affine hull
3156 * for use in coalesce.
3158 struct isl_map *isl_map_coalesce(struct isl_map *map)
3160 int i;
3161 unsigned n;
3162 isl_ctx *ctx;
3163 struct isl_coalesce_info *info = NULL;
3165 map = isl_map_remove_empty_parts(map);
3166 if (!map)
3167 return NULL;
3169 if (map->n <= 1)
3170 return map;
3172 ctx = isl_map_get_ctx(map);
3173 map = isl_map_sort_divs(map);
3174 map = isl_map_cow(map);
3176 if (!map)
3177 return NULL;
3179 n = map->n;
3181 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3182 if (!info)
3183 goto error;
3185 for (i = 0; i < map->n; ++i) {
3186 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3187 if (!map->p[i])
3188 goto error;
3189 info[i].bmap = isl_basic_map_copy(map->p[i]);
3190 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3191 if (!info[i].tab)
3192 goto error;
3193 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3194 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3195 goto error;
3196 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3197 info[i].bmap);
3198 if (!info[i].bmap)
3199 goto error;
3200 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3201 if (isl_tab_detect_redundant(info[i].tab) < 0)
3202 goto error;
3203 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3204 goto error;
3206 for (i = map->n - 1; i >= 0; --i)
3207 if (info[i].tab->empty)
3208 drop(&info[i]);
3210 if (coalesce(ctx, n, info) < 0)
3211 goto error;
3213 map = update_basic_maps(map, n, info);
3215 clear_coalesce_info(n, info);
3217 return map;
3218 error:
3219 clear_coalesce_info(n, info);
3220 isl_map_free(map);
3221 return NULL;
3224 /* For each pair of basic sets in the set, check if the union of the two
3225 * can be represented by a single basic set.
3226 * If so, replace the pair by the single basic set and start over.
3228 struct isl_set *isl_set_coalesce(struct isl_set *set)
3230 return set_from_map(isl_map_coalesce(set_to_map(set)));