python interface: expose construction of AST node from union map
[isl.git] / isl_coalesce.c
blobb7f2f90c573327da83ded679bce104b24e8f8e53
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 Sven Verdoolaege
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
19 #include <isl_ctx_private.h>
20 #include "isl_map_private.h"
21 #include <isl_seq.h>
22 #include <isl/options.h>
23 #include "isl_tab.h"
24 #include <isl_mat_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_equalities.h>
30 #define STATUS_ERROR -1
31 #define STATUS_REDUNDANT 1
32 #define STATUS_VALID 2
33 #define STATUS_SEPARATE 3
34 #define STATUS_CUT 4
35 #define STATUS_ADJ_EQ 5
36 #define STATUS_ADJ_INEQ 6
38 static int status_in(isl_int *ineq, struct isl_tab *tab)
40 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
41 switch (type) {
42 default:
43 case isl_ineq_error: return STATUS_ERROR;
44 case isl_ineq_redundant: return STATUS_VALID;
45 case isl_ineq_separate: return STATUS_SEPARATE;
46 case isl_ineq_cut: return STATUS_CUT;
47 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
48 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
52 /* Compute the position of the equalities of basic map "bmap_i"
53 * with respect to the basic map represented by "tab_j".
54 * The resulting array has twice as many entries as the number
55 * of equalities corresponding to the two inequalties to which
56 * each equality corresponds.
58 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
59 struct isl_tab *tab_j)
61 int k, l;
62 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
63 unsigned dim;
65 if (!eq)
66 return NULL;
68 dim = isl_basic_map_total_dim(bmap_i);
69 for (k = 0; k < bmap_i->n_eq; ++k) {
70 for (l = 0; l < 2; ++l) {
71 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
72 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
73 if (eq[2 * k + l] == STATUS_ERROR)
74 goto error;
76 if (eq[2 * k] == STATUS_SEPARATE ||
77 eq[2 * k + 1] == STATUS_SEPARATE)
78 break;
81 return eq;
82 error:
83 free(eq);
84 return NULL;
87 /* Compute the position of the inequalities of basic map "bmap_i"
88 * (also represented by "tab_i", if not NULL) with respect to the basic map
89 * represented by "tab_j".
91 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
92 struct isl_tab *tab_i, struct isl_tab *tab_j)
94 int k;
95 unsigned n_eq = bmap_i->n_eq;
96 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
98 if (!ineq)
99 return NULL;
101 for (k = 0; k < bmap_i->n_ineq; ++k) {
102 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
103 ineq[k] = STATUS_REDUNDANT;
104 continue;
106 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
107 if (ineq[k] == STATUS_ERROR)
108 goto error;
109 if (ineq[k] == STATUS_SEPARATE)
110 break;
113 return ineq;
114 error:
115 free(ineq);
116 return NULL;
119 static int any(int *con, unsigned len, int status)
121 int i;
123 for (i = 0; i < len ; ++i)
124 if (con[i] == status)
125 return 1;
126 return 0;
129 static int count(int *con, unsigned len, int status)
131 int i;
132 int c = 0;
134 for (i = 0; i < len ; ++i)
135 if (con[i] == status)
136 c++;
137 return c;
140 static int all(int *con, unsigned len, int status)
142 int i;
144 for (i = 0; i < len ; ++i) {
145 if (con[i] == STATUS_REDUNDANT)
146 continue;
147 if (con[i] != status)
148 return 0;
150 return 1;
153 /* Internal information associated to a basic map in a map
154 * that is to be coalesced by isl_map_coalesce.
156 * "bmap" is the basic map itself (or NULL if "removed" is set)
157 * "tab" is the corresponding tableau (or NULL if "removed" is set)
158 * "hull_hash" identifies the affine space in which "bmap" lives.
159 * "removed" is set if this basic map has been removed from the map
160 * "simplify" is set if this basic map may have some unknown integer
161 * divisions that were not present in the input basic maps. The basic
162 * map should then be simplified such that we may be able to find
163 * a definition among the constraints.
165 * "eq" and "ineq" are only set if we are currently trying to coalesce
166 * this basic map with another basic map, in which case they represent
167 * the position of the inequalities of this basic map with respect to
168 * the other basic map. The number of elements in the "eq" array
169 * is twice the number of equalities in the "bmap", corresponding
170 * to the two inequalities that make up each equality.
172 struct isl_coalesce_info {
173 isl_basic_map *bmap;
174 struct isl_tab *tab;
175 uint32_t hull_hash;
176 int removed;
177 int simplify;
178 int *eq;
179 int *ineq;
182 /* Are all non-redundant constraints of the basic map represented by "info"
183 * either valid or cut constraints with respect to the other basic map?
185 static int all_valid_or_cut(struct isl_coalesce_info *info)
187 int i;
189 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
190 if (info->eq[i] == STATUS_REDUNDANT)
191 continue;
192 if (info->eq[i] == STATUS_VALID)
193 continue;
194 if (info->eq[i] == STATUS_CUT)
195 continue;
196 return 0;
199 for (i = 0; i < info->bmap->n_ineq; ++i) {
200 if (info->ineq[i] == STATUS_REDUNDANT)
201 continue;
202 if (info->ineq[i] == STATUS_VALID)
203 continue;
204 if (info->ineq[i] == STATUS_CUT)
205 continue;
206 return 0;
209 return 1;
212 /* Compute the hash of the (apparent) affine hull of info->bmap (with
213 * the existentially quantified variables removed) and store it
214 * in info->hash.
216 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
218 isl_basic_map *hull;
219 unsigned n_div;
221 hull = isl_basic_map_copy(info->bmap);
222 hull = isl_basic_map_plain_affine_hull(hull);
223 n_div = isl_basic_map_dim(hull, isl_dim_div);
224 hull = isl_basic_map_drop_constraints_involving_dims(hull,
225 isl_dim_div, 0, n_div);
226 info->hull_hash = isl_basic_map_get_hash(hull);
227 isl_basic_map_free(hull);
229 return hull ? 0 : -1;
232 /* Free all the allocated memory in an array
233 * of "n" isl_coalesce_info elements.
235 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
237 int i;
239 if (!info)
240 return;
242 for (i = 0; i < n; ++i) {
243 isl_basic_map_free(info[i].bmap);
244 isl_tab_free(info[i].tab);
247 free(info);
250 /* Drop the basic map represented by "info".
251 * That is, clear the memory associated to the entry and
252 * mark it as having been removed.
254 static void drop(struct isl_coalesce_info *info)
256 info->bmap = isl_basic_map_free(info->bmap);
257 isl_tab_free(info->tab);
258 info->tab = NULL;
259 info->removed = 1;
262 /* Exchange the information in "info1" with that in "info2".
264 static void exchange(struct isl_coalesce_info *info1,
265 struct isl_coalesce_info *info2)
267 struct isl_coalesce_info info;
269 info = *info1;
270 *info1 = *info2;
271 *info2 = info;
274 /* This type represents the kind of change that has been performed
275 * while trying to coalesce two basic maps.
277 * isl_change_none: nothing was changed
278 * isl_change_drop_first: the first basic map was removed
279 * isl_change_drop_second: the second basic map was removed
280 * isl_change_fuse: the two basic maps were replaced by a new basic map.
282 enum isl_change {
283 isl_change_error = -1,
284 isl_change_none = 0,
285 isl_change_drop_first,
286 isl_change_drop_second,
287 isl_change_fuse,
290 /* Update "change" based on an interchange of the first and the second
291 * basic map. That is, interchange isl_change_drop_first and
292 * isl_change_drop_second.
294 static enum isl_change invert_change(enum isl_change change)
296 switch (change) {
297 case isl_change_error:
298 return isl_change_error;
299 case isl_change_none:
300 return isl_change_none;
301 case isl_change_drop_first:
302 return isl_change_drop_second;
303 case isl_change_drop_second:
304 return isl_change_drop_first;
305 case isl_change_fuse:
306 return isl_change_fuse;
309 return isl_change_error;
312 /* Add the valid constraints of the basic map represented by "info"
313 * to "bmap". "len" is the size of the constraints.
314 * If only one of the pair of inequalities that make up an equality
315 * is valid, then add that inequality.
317 static __isl_give isl_basic_map *add_valid_constraints(
318 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
319 unsigned len)
321 int k, l;
323 if (!bmap)
324 return NULL;
326 for (k = 0; k < info->bmap->n_eq; ++k) {
327 if (info->eq[2 * k] == STATUS_VALID &&
328 info->eq[2 * k + 1] == STATUS_VALID) {
329 l = isl_basic_map_alloc_equality(bmap);
330 if (l < 0)
331 return isl_basic_map_free(bmap);
332 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
333 } else if (info->eq[2 * k] == STATUS_VALID) {
334 l = isl_basic_map_alloc_inequality(bmap);
335 if (l < 0)
336 return isl_basic_map_free(bmap);
337 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
338 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
339 l = isl_basic_map_alloc_inequality(bmap);
340 if (l < 0)
341 return isl_basic_map_free(bmap);
342 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
346 for (k = 0; k < info->bmap->n_ineq; ++k) {
347 if (info->ineq[k] != STATUS_VALID)
348 continue;
349 l = isl_basic_map_alloc_inequality(bmap);
350 if (l < 0)
351 return isl_basic_map_free(bmap);
352 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
355 return bmap;
358 /* Is "bmap" defined by a number of (non-redundant) constraints that
359 * is greater than the number of constraints of basic maps i and j combined?
360 * Equalities are counted as two inequalities.
362 static int number_of_constraints_increases(int i, int j,
363 struct isl_coalesce_info *info,
364 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
366 int k, n_old, n_new;
368 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
369 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
371 n_new = 2 * bmap->n_eq;
372 for (k = 0; k < bmap->n_ineq; ++k)
373 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
374 ++n_new;
376 return n_new > n_old;
379 /* Replace the pair of basic maps i and j by the basic map bounded
380 * by the valid constraints in both basic maps and the constraints
381 * in extra (if not NULL).
382 * Place the fused basic map in the position that is the smallest of i and j.
384 * If "detect_equalities" is set, then look for equalities encoded
385 * as pairs of inequalities.
386 * If "check_number" is set, then the original basic maps are only
387 * replaced if the total number of constraints does not increase.
388 * While the number of integer divisions in the two basic maps
389 * is assumed to be the same, the actual definitions may be different.
390 * We only copy the definition from one of the basic map if it is
391 * the same as that of the other basic map. Otherwise, we mark
392 * the integer division as unknown and simplify the basic map
393 * in an attempt to recover the integer division definition.
395 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
396 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
398 int k, l;
399 struct isl_basic_map *fused = NULL;
400 struct isl_tab *fused_tab = NULL;
401 unsigned total = isl_basic_map_total_dim(info[i].bmap);
402 unsigned extra_rows = extra ? extra->n_row : 0;
403 unsigned n_eq, n_ineq;
404 int simplify = 0;
406 if (j < i)
407 return fuse(j, i, info, extra, detect_equalities, check_number);
409 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
410 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
411 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
412 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
413 fused = add_valid_constraints(fused, &info[i], 1 + total);
414 fused = add_valid_constraints(fused, &info[j], 1 + total);
415 if (!fused)
416 goto error;
418 for (k = 0; k < info[i].bmap->n_div; ++k) {
419 int l = isl_basic_map_alloc_div(fused);
420 if (l < 0)
421 goto error;
422 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
423 1 + 1 + total)) {
424 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
425 1 + 1 + total);
426 } else {
427 isl_int_set_si(fused->div[l][0], 0);
428 simplify = 1;
432 for (k = 0; k < extra_rows; ++k) {
433 l = isl_basic_map_alloc_inequality(fused);
434 if (l < 0)
435 goto error;
436 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
439 if (detect_equalities)
440 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
441 fused = isl_basic_map_gauss(fused, NULL);
442 if (simplify || info[j].simplify) {
443 fused = isl_basic_map_simplify(fused);
444 info[i].simplify = 0;
446 fused = isl_basic_map_finalize(fused);
447 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
448 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
449 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
451 fused_tab = isl_tab_from_basic_map(fused, 0);
452 if (isl_tab_detect_redundant(fused_tab) < 0)
453 goto error;
455 if (check_number &&
456 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
457 isl_tab_free(fused_tab);
458 isl_basic_map_free(fused);
459 return isl_change_none;
462 isl_basic_map_free(info[i].bmap);
463 info[i].bmap = fused;
464 isl_tab_free(info[i].tab);
465 info[i].tab = fused_tab;
466 drop(&info[j]);
468 return isl_change_fuse;
469 error:
470 isl_tab_free(fused_tab);
471 isl_basic_map_free(fused);
472 return isl_change_error;
475 /* Given a pair of basic maps i and j such that all constraints are either
476 * "valid" or "cut", check if the facets corresponding to the "cut"
477 * constraints of i lie entirely within basic map j.
478 * If so, replace the pair by the basic map consisting of the valid
479 * constraints in both basic maps.
480 * Checking whether the facet lies entirely within basic map j
481 * is performed by checking whether the constraints of basic map j
482 * are valid for the facet. These tests are performed on a rational
483 * tableau to avoid the theoretical possibility that a constraint
484 * that was considered to be a cut constraint for the entire basic map i
485 * happens to be considered to be a valid constraint for the facet,
486 * even though it cuts off the same rational points.
488 * To see that we are not introducing any extra points, call the
489 * two basic maps A and B and the resulting map U and let x
490 * be an element of U \setminus ( A \cup B ).
491 * A line connecting x with an element of A \cup B meets a facet F
492 * of either A or B. Assume it is a facet of B and let c_1 be
493 * the corresponding facet constraint. We have c_1(x) < 0 and
494 * so c_1 is a cut constraint. This implies that there is some
495 * (possibly rational) point x' satisfying the constraints of A
496 * and the opposite of c_1 as otherwise c_1 would have been marked
497 * valid for A. The line connecting x and x' meets a facet of A
498 * in a (possibly rational) point that also violates c_1, but this
499 * is impossible since all cut constraints of B are valid for all
500 * cut facets of A.
501 * In case F is a facet of A rather than B, then we can apply the
502 * above reasoning to find a facet of B separating x from A \cup B first.
504 static enum isl_change check_facets(int i, int j,
505 struct isl_coalesce_info *info)
507 int k, l;
508 struct isl_tab_undo *snap, *snap2;
509 unsigned n_eq = info[i].bmap->n_eq;
511 snap = isl_tab_snap(info[i].tab);
512 if (isl_tab_mark_rational(info[i].tab) < 0)
513 return isl_change_error;
514 snap2 = isl_tab_snap(info[i].tab);
516 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
517 if (info[i].ineq[k] != STATUS_CUT)
518 continue;
519 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
520 return isl_change_error;
521 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
522 int stat;
523 if (info[j].ineq[l] != STATUS_CUT)
524 continue;
525 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
526 if (stat < 0)
527 return isl_change_error;
528 if (stat != STATUS_VALID)
529 break;
531 if (isl_tab_rollback(info[i].tab, snap2) < 0)
532 return isl_change_error;
533 if (l < info[j].bmap->n_ineq)
534 break;
537 if (k < info[i].bmap->n_ineq) {
538 if (isl_tab_rollback(info[i].tab, snap) < 0)
539 return isl_change_error;
540 return isl_change_none;
542 return fuse(i, j, info, NULL, 0, 0);
545 /* Check if info->bmap contains the basic map represented
546 * by the tableau "tab".
547 * For each equality, we check both the constraint itself
548 * (as an inequality) and its negation. Make sure the
549 * equality is returned to its original state before returning.
551 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
553 int k;
554 unsigned dim;
555 isl_basic_map *bmap = info->bmap;
557 dim = isl_basic_map_total_dim(bmap);
558 for (k = 0; k < bmap->n_eq; ++k) {
559 int stat;
560 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
561 stat = status_in(bmap->eq[k], tab);
562 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
563 if (stat < 0)
564 return -1;
565 if (stat != STATUS_VALID)
566 return 0;
567 stat = status_in(bmap->eq[k], tab);
568 if (stat < 0)
569 return -1;
570 if (stat != STATUS_VALID)
571 return 0;
574 for (k = 0; k < bmap->n_ineq; ++k) {
575 int stat;
576 if (info->ineq[k] == STATUS_REDUNDANT)
577 continue;
578 stat = status_in(bmap->ineq[k], tab);
579 if (stat < 0)
580 return -1;
581 if (stat != STATUS_VALID)
582 return 0;
584 return 1;
587 /* Basic map "i" has an inequality (say "k") that is adjacent
588 * to some inequality of basic map "j". All the other inequalities
589 * are valid for "j".
590 * Check if basic map "j" forms an extension of basic map "i".
592 * Note that this function is only called if some of the equalities or
593 * inequalities of basic map "j" do cut basic map "i". The function is
594 * correct even if there are no such cut constraints, but in that case
595 * the additional checks performed by this function are overkill.
597 * In particular, we replace constraint k, say f >= 0, by constraint
598 * f <= -1, add the inequalities of "j" that are valid for "i"
599 * and check if the result is a subset of basic map "j".
600 * If so, then we know that this result is exactly equal to basic map "j"
601 * since all its constraints are valid for basic map "j".
602 * By combining the valid constraints of "i" (all equalities and all
603 * inequalities except "k") and the valid constraints of "j" we therefore
604 * obtain a basic map that is equal to their union.
605 * In this case, there is no need to perform a rollback of the tableau
606 * since it is going to be destroyed in fuse().
609 * |\__ |\__
610 * | \__ | \__
611 * | \_ => | \__
612 * |_______| _ |_________\
615 * |\ |\
616 * | \ | \
617 * | \ | \
618 * | | | \
619 * | ||\ => | \
620 * | || \ | \
621 * | || | | |
622 * |__||_/ |_____/
624 static enum isl_change is_adj_ineq_extension(int i, int j,
625 struct isl_coalesce_info *info)
627 int k;
628 struct isl_tab_undo *snap;
629 unsigned n_eq = info[i].bmap->n_eq;
630 unsigned total = isl_basic_map_total_dim(info[i].bmap);
631 int r;
632 int super;
634 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
635 return isl_change_error;
637 for (k = 0; k < info[i].bmap->n_ineq; ++k)
638 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
639 break;
640 if (k >= info[i].bmap->n_ineq)
641 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
642 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
643 return isl_change_error);
645 snap = isl_tab_snap(info[i].tab);
647 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
648 return isl_change_error;
650 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
651 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
652 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
653 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
654 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
655 if (r < 0)
656 return isl_change_error;
658 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
659 if (info[j].ineq[k] != STATUS_VALID)
660 continue;
661 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
662 return isl_change_error;
665 super = contains(&info[j], info[i].tab);
666 if (super < 0)
667 return isl_change_error;
668 if (super)
669 return fuse(i, j, info, NULL, 0, 0);
671 if (isl_tab_rollback(info[i].tab, snap) < 0)
672 return isl_change_error;
674 return isl_change_none;
678 /* Both basic maps have at least one inequality with and adjacent
679 * (but opposite) inequality in the other basic map.
680 * Check that there are no cut constraints and that there is only
681 * a single pair of adjacent inequalities.
682 * If so, we can replace the pair by a single basic map described
683 * by all but the pair of adjacent inequalities.
684 * Any additional points introduced lie strictly between the two
685 * adjacent hyperplanes and can therefore be integral.
687 * ____ _____
688 * / ||\ / \
689 * / || \ / \
690 * \ || \ => \ \
691 * \ || / \ /
692 * \___||_/ \_____/
694 * The test for a single pair of adjancent inequalities is important
695 * for avoiding the combination of two basic maps like the following
697 * /|
698 * / |
699 * /__|
700 * _____
701 * | |
702 * | |
703 * |___|
705 * If there are some cut constraints on one side, then we may
706 * still be able to fuse the two basic maps, but we need to perform
707 * some additional checks in is_adj_ineq_extension.
709 static enum isl_change check_adj_ineq(int i, int j,
710 struct isl_coalesce_info *info)
712 int count_i, count_j;
713 int cut_i, cut_j;
715 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
716 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
718 if (count_i != 1 && count_j != 1)
719 return isl_change_none;
721 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
722 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
723 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
724 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
726 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
727 return fuse(i, j, info, NULL, 0, 0);
729 if (count_i == 1 && !cut_i)
730 return is_adj_ineq_extension(i, j, info);
732 if (count_j == 1 && !cut_j)
733 return is_adj_ineq_extension(j, i, info);
735 return isl_change_none;
738 /* Given an affine transformation matrix "T", does row "row" represent
739 * anything other than a unit vector (possibly shifted by a constant)
740 * that is not involved in any of the other rows?
742 * That is, if a constraint involves the variable corresponding to
743 * the row, then could its preimage by "T" have any coefficients
744 * that are different from those in the original constraint?
746 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
748 int i, j;
749 int len = T->n_col - 1;
751 i = isl_seq_first_non_zero(T->row[row] + 1, len);
752 if (i < 0)
753 return 1;
754 if (!isl_int_is_one(T->row[row][1 + i]) &&
755 !isl_int_is_negone(T->row[row][1 + i]))
756 return 1;
758 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
759 if (j >= 0)
760 return 1;
762 for (j = 1; j < T->n_row; ++j) {
763 if (j == row)
764 continue;
765 if (!isl_int_is_zero(T->row[j][1 + i]))
766 return 1;
769 return 0;
772 /* Does inequality constraint "ineq" of "bmap" involve any of
773 * the variables marked in "affected"?
774 * "total" is the total number of variables, i.e., the number
775 * of entries in "affected".
777 static int is_affected(__isl_keep isl_basic_map *bmap, int ineq, int *affected,
778 int total)
780 int i;
782 for (i = 0; i < total; ++i) {
783 if (!affected[i])
784 continue;
785 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
786 return 1;
789 return 0;
792 /* Given the compressed version of inequality constraint "ineq"
793 * of info->bmap in "v", check if the constraint can be tightened,
794 * where the compression is based on an equality constraint valid
795 * for info->tab.
796 * If so, add the tightened version of the inequality constraint
797 * to info->tab. "v" may be modified by this function.
799 * That is, if the compressed constraint is of the form
801 * m f() + c >= 0
803 * with 0 < c < m, then it is equivalent to
805 * f() >= 0
807 * This means that c can also be subtracted from the original,
808 * uncompressed constraint without affecting the integer points
809 * in info->tab. Add this tightened constraint as an extra row
810 * to info->tab to make this information explicitly available.
812 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
813 int ineq, __isl_take isl_vec *v)
815 isl_ctx *ctx;
816 int r;
818 if (!v)
819 return NULL;
821 ctx = isl_vec_get_ctx(v);
822 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
823 if (isl_int_is_zero(ctx->normalize_gcd) ||
824 isl_int_is_one(ctx->normalize_gcd)) {
825 return v;
828 v = isl_vec_cow(v);
829 if (!v)
830 return NULL;
832 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
833 if (isl_int_is_zero(v->el[0]))
834 return v;
836 if (isl_tab_extend_cons(info->tab, 1) < 0)
837 return isl_vec_free(v);
839 isl_int_sub(info->bmap->ineq[ineq][0],
840 info->bmap->ineq[ineq][0], v->el[0]);
841 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
842 isl_int_add(info->bmap->ineq[ineq][0],
843 info->bmap->ineq[ineq][0], v->el[0]);
845 if (r < 0)
846 return isl_vec_free(v);
848 return v;
851 /* Tighten the constraints on the facet represented by info->tab.
852 * In particular, on input, info->tab represents the result
853 * of replacing constraint k of info->bmap, i.e., f_k >= 0,
854 * by the adjacent equality, i.e., f_k + 1 = 0.
856 * Compute a variable compression from the equality constraint f_k + 1 = 0
857 * and use it to tighten the other constraints of info->bmap,
858 * updating info->tab (and leaving info->bmap untouched).
859 * The compression handles essentially two cases, one where a variable
860 * is assigned a fixed value and can therefore be eliminated, and one
861 * where one variable is a shifted multiple of some other variable and
862 * can therefore be replaced by that multiple.
863 * Gaussian elimination would also work for the first case, but for
864 * the second case, the effectiveness would depend on the order
865 * of the variables.
866 * After compression, some of the constraints may have coefficients
867 * with a common divisor. If this divisor does not divide the constant
868 * term, then the constraint can be tightened.
869 * The tightening is performed on the tableau info->tab by introducing
870 * extra (temporary) constraints.
872 * Only constraints that are possibly affected by the compression are
873 * considered. In particular, if the constraint only involves variables
874 * that are directly mapped to a distinct set of other variables, then
875 * no common divisor can be introduced and no tightening can occur.
877 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
878 int k)
880 unsigned total;
881 isl_ctx *ctx;
882 isl_vec *v = NULL;
883 isl_mat *T;
884 int i;
885 int *affected;
887 ctx = isl_basic_map_get_ctx(info->bmap);
888 total = isl_basic_map_total_dim(info->bmap);
889 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
890 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
891 T = isl_mat_variable_compression(T, NULL);
892 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
893 if (!T)
894 return isl_stat_error;
895 if (T->n_col == 0) {
896 isl_mat_free(T);
897 return isl_stat_ok;
900 affected = isl_alloc_array(ctx, int, total);
901 if (!affected)
902 goto error;
904 for (i = 0; i < total; ++i)
905 affected[i] = not_unique_unit_row(T, 1 + i);
907 for (i = 0; i < info->bmap->n_ineq; ++i) {
908 if (i == k)
909 continue;
910 if (!is_affected(info->bmap, i, affected, total))
911 continue;
912 v = isl_vec_alloc(ctx, 1 + total);
913 if (!v)
914 goto error;
915 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
916 v = isl_vec_mat_product(v, isl_mat_copy(T));
917 v = try_tightening(info, i, v);
918 isl_vec_free(v);
919 if (!v)
920 goto error;
923 isl_mat_free(T);
924 free(affected);
925 return isl_stat_ok;
926 error:
927 isl_mat_free(T);
928 free(affected);
929 return isl_stat_error;
932 /* Basic map "i" has an inequality "k" that is adjacent to some equality
933 * of basic map "j". All the other inequalities are valid for "j".
934 * Check if basic map "j" forms an extension of basic map "i".
936 * In particular, we relax constraint "k", compute the corresponding
937 * facet and check whether it is included in the other basic map.
938 * Before testing for inclusion, the constraints on the facet
939 * are tightened to increase the chance of an inclusion being detected.
940 * If the facet is included, we know that relaxing the constraint extends
941 * the basic map with exactly the other basic map (we already know that this
942 * other basic map is included in the extension, because there
943 * were no "cut" inequalities in "i") and we can replace the
944 * two basic maps by this extension.
945 * Each integer division that does not have exactly the same
946 * definition in "i" and "j" is marked unknown and the basic map
947 * is scheduled to be simplified in an attempt to recover
948 * the integer division definition.
949 * Place this extension in the position that is the smallest of i and j.
950 * ____ _____
951 * / || / |
952 * / || / |
953 * \ || => \ |
954 * \ || \ |
955 * \___|| \____|
957 static enum isl_change is_adj_eq_extension(int i, int j, int k,
958 struct isl_coalesce_info *info)
960 int change = isl_change_none;
961 int super;
962 struct isl_tab_undo *snap, *snap2;
963 unsigned n_eq = info[i].bmap->n_eq;
965 if (isl_tab_is_equality(info[i].tab, n_eq + k))
966 return isl_change_none;
968 snap = isl_tab_snap(info[i].tab);
969 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
970 return isl_change_error;
971 snap2 = isl_tab_snap(info[i].tab);
972 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
973 return isl_change_error;
974 if (tighten_on_relaxed_facet(&info[i], k) < 0)
975 return isl_change_error;
976 super = contains(&info[j], info[i].tab);
977 if (super < 0)
978 return isl_change_error;
979 if (super) {
980 int l;
981 unsigned total;
983 if (isl_tab_rollback(info[i].tab, snap2) < 0)
984 return isl_change_error;
985 info[i].bmap = isl_basic_map_cow(info[i].bmap);
986 if (!info[i].bmap)
987 return isl_change_error;
988 total = isl_basic_map_total_dim(info[i].bmap);
989 for (l = 0; l < info[i].bmap->n_div; ++l)
990 if (!isl_seq_eq(info[i].bmap->div[l],
991 info[j].bmap->div[l], 1 + 1 + total)) {
992 isl_int_set_si(info[i].bmap->div[l][0], 0);
993 info[i].simplify = 1;
995 isl_int_add_ui(info[i].bmap->ineq[k][0],
996 info[i].bmap->ineq[k][0], 1);
997 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
998 drop(&info[j]);
999 if (j < i)
1000 exchange(&info[i], &info[j]);
1001 change = isl_change_fuse;
1002 } else
1003 if (isl_tab_rollback(info[i].tab, snap) < 0)
1004 return isl_change_error;
1006 return change;
1009 /* Data structure that keeps track of the wrapping constraints
1010 * and of information to bound the coefficients of those constraints.
1012 * bound is set if we want to apply a bound on the coefficients
1013 * mat contains the wrapping constraints
1014 * max is the bound on the coefficients (if bound is set)
1016 struct isl_wraps {
1017 int bound;
1018 isl_mat *mat;
1019 isl_int max;
1022 /* Update wraps->max to be greater than or equal to the coefficients
1023 * in the equalities and inequalities of info->bmap that can be removed
1024 * if we end up applying wrapping.
1026 static void wraps_update_max(struct isl_wraps *wraps,
1027 struct isl_coalesce_info *info)
1029 int k;
1030 isl_int max_k;
1031 unsigned total = isl_basic_map_total_dim(info->bmap);
1033 isl_int_init(max_k);
1035 for (k = 0; k < info->bmap->n_eq; ++k) {
1036 if (info->eq[2 * k] == STATUS_VALID &&
1037 info->eq[2 * k + 1] == STATUS_VALID)
1038 continue;
1039 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1040 if (isl_int_abs_gt(max_k, wraps->max))
1041 isl_int_set(wraps->max, max_k);
1044 for (k = 0; k < info->bmap->n_ineq; ++k) {
1045 if (info->ineq[k] == STATUS_VALID ||
1046 info->ineq[k] == STATUS_REDUNDANT)
1047 continue;
1048 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1049 if (isl_int_abs_gt(max_k, wraps->max))
1050 isl_int_set(wraps->max, max_k);
1053 isl_int_clear(max_k);
1056 /* Initialize the isl_wraps data structure.
1057 * If we want to bound the coefficients of the wrapping constraints,
1058 * we set wraps->max to the largest coefficient
1059 * in the equalities and inequalities that can be removed if we end up
1060 * applying wrapping.
1062 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1063 struct isl_coalesce_info *info, int i, int j)
1065 isl_ctx *ctx;
1067 wraps->bound = 0;
1068 wraps->mat = mat;
1069 if (!mat)
1070 return;
1071 ctx = isl_mat_get_ctx(mat);
1072 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1073 if (!wraps->bound)
1074 return;
1075 isl_int_init(wraps->max);
1076 isl_int_set_si(wraps->max, 0);
1077 wraps_update_max(wraps, &info[i]);
1078 wraps_update_max(wraps, &info[j]);
1081 /* Free the contents of the isl_wraps data structure.
1083 static void wraps_free(struct isl_wraps *wraps)
1085 isl_mat_free(wraps->mat);
1086 if (wraps->bound)
1087 isl_int_clear(wraps->max);
1090 /* Is the wrapping constraint in row "row" allowed?
1092 * If wraps->bound is set, we check that none of the coefficients
1093 * is greater than wraps->max.
1095 static int allow_wrap(struct isl_wraps *wraps, int row)
1097 int i;
1099 if (!wraps->bound)
1100 return 1;
1102 for (i = 1; i < wraps->mat->n_col; ++i)
1103 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1104 return 0;
1106 return 1;
1109 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1110 * to include "set" and add the result in position "w" of "wraps".
1111 * "len" is the total number of coefficients in "bound" and "ineq".
1112 * Return 1 on success, 0 on failure and -1 on error.
1113 * Wrapping can fail if the result of wrapping is equal to "bound"
1114 * or if we want to bound the sizes of the coefficients and
1115 * the wrapped constraint does not satisfy this bound.
1117 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1118 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1120 isl_seq_cpy(wraps->mat->row[w], bound, len);
1121 if (negate) {
1122 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1123 ineq = wraps->mat->row[w + 1];
1125 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1126 return -1;
1127 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1128 return 0;
1129 if (!allow_wrap(wraps, w))
1130 return 0;
1131 return 1;
1134 /* For each constraint in info->bmap that is not redundant (as determined
1135 * by info->tab) and that is not a valid constraint for the other basic map,
1136 * wrap the constraint around "bound" such that it includes the whole
1137 * set "set" and append the resulting constraint to "wraps".
1138 * Note that the constraints that are valid for the other basic map
1139 * will be added to the combined basic map by default, so there is
1140 * no need to wrap them.
1141 * The caller wrap_in_facets even relies on this function not wrapping
1142 * any constraints that are already valid.
1143 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1144 * wraps->n_row is the number of actual wrapped constraints that have
1145 * been added.
1146 * If any of the wrapping problems results in a constraint that is
1147 * identical to "bound", then this means that "set" is unbounded in such
1148 * way that no wrapping is possible. If this happens then wraps->n_row
1149 * is reset to zero.
1150 * Similarly, if we want to bound the coefficients of the wrapping
1151 * constraints and a newly added wrapping constraint does not
1152 * satisfy the bound, then wraps->n_row is also reset to zero.
1154 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1155 isl_int *bound, __isl_keep isl_set *set)
1157 int l, m;
1158 int w;
1159 int added;
1160 isl_basic_map *bmap = info->bmap;
1161 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1163 w = wraps->mat->n_row;
1165 for (l = 0; l < bmap->n_ineq; ++l) {
1166 if (info->ineq[l] == STATUS_VALID ||
1167 info->ineq[l] == STATUS_REDUNDANT)
1168 continue;
1169 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1170 continue;
1171 if (isl_seq_eq(bound, bmap->ineq[l], len))
1172 continue;
1173 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1174 continue;
1176 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1177 if (added < 0)
1178 return -1;
1179 if (!added)
1180 goto unbounded;
1181 ++w;
1183 for (l = 0; l < bmap->n_eq; ++l) {
1184 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1185 continue;
1186 if (isl_seq_eq(bound, bmap->eq[l], len))
1187 continue;
1189 for (m = 0; m < 2; ++m) {
1190 if (info->eq[2 * l + m] == STATUS_VALID)
1191 continue;
1192 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1193 set, !m);
1194 if (added < 0)
1195 return -1;
1196 if (!added)
1197 goto unbounded;
1198 ++w;
1202 wraps->mat->n_row = w;
1203 return 0;
1204 unbounded:
1205 wraps->mat->n_row = 0;
1206 return 0;
1209 /* Check if the constraints in "wraps" from "first" until the last
1210 * are all valid for the basic set represented by "tab".
1211 * If not, wraps->n_row is set to zero.
1213 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1214 struct isl_tab *tab)
1216 int i;
1218 for (i = first; i < wraps->n_row; ++i) {
1219 enum isl_ineq_type type;
1220 type = isl_tab_ineq_type(tab, wraps->row[i]);
1221 if (type == isl_ineq_error)
1222 return -1;
1223 if (type == isl_ineq_redundant)
1224 continue;
1225 wraps->n_row = 0;
1226 return 0;
1229 return 0;
1232 /* Return a set that corresponds to the non-redundant constraints
1233 * (as recorded in tab) of bmap.
1235 * It's important to remove the redundant constraints as some
1236 * of the other constraints may have been modified after the
1237 * constraints were marked redundant.
1238 * In particular, a constraint may have been relaxed.
1239 * Redundant constraints are ignored when a constraint is relaxed
1240 * and should therefore continue to be ignored ever after.
1241 * Otherwise, the relaxation might be thwarted by some of
1242 * these constraints.
1244 * Update the underlying set to ensure that the dimension doesn't change.
1245 * Otherwise the integer divisions could get dropped if the tab
1246 * turns out to be empty.
1248 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1249 struct isl_tab *tab)
1251 isl_basic_set *bset;
1253 bmap = isl_basic_map_copy(bmap);
1254 bset = isl_basic_map_underlying_set(bmap);
1255 bset = isl_basic_set_cow(bset);
1256 bset = isl_basic_set_update_from_tab(bset, tab);
1257 return isl_set_from_basic_set(bset);
1260 /* Wrap the constraints of info->bmap that bound the facet defined
1261 * by inequality "k" around (the opposite of) this inequality to
1262 * include "set". "bound" may be used to store the negated inequality.
1263 * Since the wrapped constraints are not guaranteed to contain the whole
1264 * of info->bmap, we check them in check_wraps.
1265 * If any of the wrapped constraints turn out to be invalid, then
1266 * check_wraps will reset wrap->n_row to zero.
1268 static int add_wraps_around_facet(struct isl_wraps *wraps,
1269 struct isl_coalesce_info *info, int k, isl_int *bound,
1270 __isl_keep isl_set *set)
1272 struct isl_tab_undo *snap;
1273 int n;
1274 unsigned total = isl_basic_map_total_dim(info->bmap);
1276 snap = isl_tab_snap(info->tab);
1278 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1279 return -1;
1280 if (isl_tab_detect_redundant(info->tab) < 0)
1281 return -1;
1283 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1285 n = wraps->mat->n_row;
1286 if (add_wraps(wraps, info, bound, set) < 0)
1287 return -1;
1289 if (isl_tab_rollback(info->tab, snap) < 0)
1290 return -1;
1291 if (check_wraps(wraps->mat, n, info->tab) < 0)
1292 return -1;
1294 return 0;
1297 /* Given a basic set i with a constraint k that is adjacent to
1298 * basic set j, check if we can wrap
1299 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1300 * (always) around their ridges to include the other set.
1301 * If so, replace the pair of basic sets by their union.
1303 * All constraints of i (except k) are assumed to be valid or
1304 * cut constraints for j.
1305 * Wrapping the cut constraints to include basic map j may result
1306 * in constraints that are no longer valid of basic map i
1307 * we have to check that the resulting wrapping constraints are valid for i.
1308 * If "wrap_facet" is not set, then all constraints of i (except k)
1309 * are assumed to be valid for j.
1310 * ____ _____
1311 * / | / \
1312 * / || / |
1313 * \ || => \ |
1314 * \ || \ |
1315 * \___|| \____|
1318 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1319 struct isl_coalesce_info *info, int wrap_facet)
1321 enum isl_change change = isl_change_none;
1322 struct isl_wraps wraps;
1323 isl_ctx *ctx;
1324 isl_mat *mat;
1325 struct isl_set *set_i = NULL;
1326 struct isl_set *set_j = NULL;
1327 struct isl_vec *bound = NULL;
1328 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1330 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1331 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1332 ctx = isl_basic_map_get_ctx(info[i].bmap);
1333 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1334 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1335 1 + total);
1336 wraps_init(&wraps, mat, info, i, j);
1337 bound = isl_vec_alloc(ctx, 1 + total);
1338 if (!set_i || !set_j || !wraps.mat || !bound)
1339 goto error;
1341 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1342 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1344 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1345 wraps.mat->n_row = 1;
1347 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1348 goto error;
1349 if (!wraps.mat->n_row)
1350 goto unbounded;
1352 if (wrap_facet) {
1353 if (add_wraps_around_facet(&wraps, &info[i], k,
1354 bound->el, set_j) < 0)
1355 goto error;
1356 if (!wraps.mat->n_row)
1357 goto unbounded;
1360 change = fuse(i, j, info, wraps.mat, 0, 0);
1362 unbounded:
1363 wraps_free(&wraps);
1365 isl_set_free(set_i);
1366 isl_set_free(set_j);
1368 isl_vec_free(bound);
1370 return change;
1371 error:
1372 wraps_free(&wraps);
1373 isl_vec_free(bound);
1374 isl_set_free(set_i);
1375 isl_set_free(set_j);
1376 return isl_change_error;
1379 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1380 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1381 * add wrapping constraints to wrap.mat for all constraints
1382 * of basic map j that bound the part of basic map j that sticks out
1383 * of the cut constraint.
1384 * "set_i" is the underlying set of basic map i.
1385 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1387 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1388 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1389 * (with respect to the integer points), so we add t(x) >= 0 instead.
1390 * Otherwise, we wrap the constraints of basic map j that are not
1391 * redundant in this intersection and that are not already valid
1392 * for basic map i over basic map i.
1393 * Note that it is sufficient to wrap the constraints to include
1394 * basic map i, because we will only wrap the constraints that do
1395 * not include basic map i already. The wrapped constraint will
1396 * therefore be more relaxed compared to the original constraint.
1397 * Since the original constraint is valid for basic map j, so is
1398 * the wrapped constraint.
1400 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1401 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1402 struct isl_tab_undo *snap)
1404 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1405 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1406 return isl_stat_error;
1407 if (isl_tab_detect_redundant(info_j->tab) < 0)
1408 return isl_stat_error;
1410 if (info_j->tab->empty)
1411 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1412 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1413 return isl_stat_error;
1415 if (isl_tab_rollback(info_j->tab, snap) < 0)
1416 return isl_stat_error;
1418 return isl_stat_ok;
1421 /* Given a pair of basic maps i and j such that j sticks out
1422 * of i at n cut constraints, each time by at most one,
1423 * try to compute wrapping constraints and replace the two
1424 * basic maps by a single basic map.
1425 * The other constraints of i are assumed to be valid for j.
1426 * "set_i" is the underlying set of basic map i.
1427 * "wraps" has been initialized to be of the right size.
1429 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1430 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1431 * of basic map j that bound the part of basic map j that sticks out
1432 * of the cut constraint.
1434 * If any wrapping fails, i.e., if we cannot wrap to touch
1435 * the union, then we give up.
1436 * Otherwise, the pair of basic maps is replaced by their union.
1438 static enum isl_change try_wrap_in_facets(int i, int j,
1439 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1440 __isl_keep isl_set *set_i)
1442 int k, l, w;
1443 unsigned total;
1444 struct isl_tab_undo *snap;
1446 total = isl_basic_map_total_dim(info[i].bmap);
1448 snap = isl_tab_snap(info[j].tab);
1450 wraps->mat->n_row = 0;
1452 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1453 for (l = 0; l < 2; ++l) {
1454 if (info[i].eq[2 * k + l] != STATUS_CUT)
1455 continue;
1456 w = wraps->mat->n_row++;
1457 if (l == 0)
1458 isl_seq_neg(wraps->mat->row[w],
1459 info[i].bmap->eq[k], 1 + total);
1460 else
1461 isl_seq_cpy(wraps->mat->row[w],
1462 info[i].bmap->eq[k], 1 + total);
1463 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1464 return isl_change_error;
1466 if (!wraps->mat->n_row)
1467 return isl_change_none;
1471 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1472 if (info[i].ineq[k] != STATUS_CUT)
1473 continue;
1474 w = wraps->mat->n_row++;
1475 isl_seq_cpy(wraps->mat->row[w],
1476 info[i].bmap->ineq[k], 1 + total);
1477 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1478 return isl_change_error;
1480 if (!wraps->mat->n_row)
1481 return isl_change_none;
1484 return fuse(i, j, info, wraps->mat, 0, 1);
1487 /* Given a pair of basic maps i and j such that j sticks out
1488 * of i at n cut constraints, each time by at most one,
1489 * try to compute wrapping constraints and replace the two
1490 * basic maps by a single basic map.
1491 * The other constraints of i are assumed to be valid for j.
1493 * The core computation is performed by try_wrap_in_facets.
1494 * This function simply extracts an underlying set representation
1495 * of basic map i and initializes the data structure for keeping
1496 * track of wrapping constraints.
1498 static enum isl_change wrap_in_facets(int i, int j, int n,
1499 struct isl_coalesce_info *info)
1501 enum isl_change change = isl_change_none;
1502 struct isl_wraps wraps;
1503 isl_ctx *ctx;
1504 isl_mat *mat;
1505 isl_set *set_i = NULL;
1506 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1507 int max_wrap;
1509 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1510 return isl_change_error;
1512 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1513 max_wrap *= n;
1515 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1516 ctx = isl_basic_map_get_ctx(info[i].bmap);
1517 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1518 wraps_init(&wraps, mat, info, i, j);
1519 if (!set_i || !wraps.mat)
1520 goto error;
1522 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1524 wraps_free(&wraps);
1525 isl_set_free(set_i);
1527 return change;
1528 error:
1529 wraps_free(&wraps);
1530 isl_set_free(set_i);
1531 return isl_change_error;
1534 /* Return the effect of inequality "ineq" on the tableau "tab",
1535 * after relaxing the constant term of "ineq" by one.
1537 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1539 enum isl_ineq_type type;
1541 isl_int_add_ui(ineq[0], ineq[0], 1);
1542 type = isl_tab_ineq_type(tab, ineq);
1543 isl_int_sub_ui(ineq[0], ineq[0], 1);
1545 return type;
1548 /* Given two basic sets i and j,
1549 * check if relaxing all the cut constraints of i by one turns
1550 * them into valid constraint for j and check if we can wrap in
1551 * the bits that are sticking out.
1552 * If so, replace the pair by their union.
1554 * We first check if all relaxed cut inequalities of i are valid for j
1555 * and then try to wrap in the intersections of the relaxed cut inequalities
1556 * with j.
1558 * During this wrapping, we consider the points of j that lie at a distance
1559 * of exactly 1 from i. In particular, we ignore the points that lie in
1560 * between this lower-dimensional space and the basic map i.
1561 * We can therefore only apply this to integer maps.
1562 * ____ _____
1563 * / ___|_ / \
1564 * / | | / |
1565 * \ | | => \ |
1566 * \|____| \ |
1567 * \___| \____/
1569 * _____ ______
1570 * | ____|_ | \
1571 * | | | | |
1572 * | | | => | |
1573 * |_| | | |
1574 * |_____| \______|
1576 * _______
1577 * | |
1578 * | |\ |
1579 * | | \ |
1580 * | | \ |
1581 * | | \|
1582 * | | \
1583 * | |_____\
1584 * | |
1585 * |_______|
1587 * Wrapping can fail if the result of wrapping one of the facets
1588 * around its edges does not produce any new facet constraint.
1589 * In particular, this happens when we try to wrap in unbounded sets.
1591 * _______________________________________________________________________
1593 * | ___
1594 * | | |
1595 * |_| |_________________________________________________________________
1596 * |___|
1598 * The following is not an acceptable result of coalescing the above two
1599 * sets as it includes extra integer points.
1600 * _______________________________________________________________________
1602 * |
1603 * |
1605 * \______________________________________________________________________
1607 static enum isl_change can_wrap_in_set(int i, int j,
1608 struct isl_coalesce_info *info)
1610 int k, l;
1611 int n;
1612 unsigned total;
1614 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1615 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1616 return isl_change_none;
1618 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1619 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1620 if (n == 0)
1621 return isl_change_none;
1623 total = isl_basic_map_total_dim(info[i].bmap);
1624 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1625 for (l = 0; l < 2; ++l) {
1626 enum isl_ineq_type type;
1628 if (info[i].eq[2 * k + l] != STATUS_CUT)
1629 continue;
1631 if (l == 0)
1632 isl_seq_neg(info[i].bmap->eq[k],
1633 info[i].bmap->eq[k], 1 + total);
1634 type = type_of_relaxed(info[j].tab,
1635 info[i].bmap->eq[k]);
1636 if (l == 0)
1637 isl_seq_neg(info[i].bmap->eq[k],
1638 info[i].bmap->eq[k], 1 + total);
1639 if (type == isl_ineq_error)
1640 return isl_change_error;
1641 if (type != isl_ineq_redundant)
1642 return isl_change_none;
1646 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1647 enum isl_ineq_type type;
1649 if (info[i].ineq[k] != STATUS_CUT)
1650 continue;
1652 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1653 if (type == isl_ineq_error)
1654 return isl_change_error;
1655 if (type != isl_ineq_redundant)
1656 return isl_change_none;
1659 return wrap_in_facets(i, j, n, info);
1662 /* Check if either i or j has only cut constraints that can
1663 * be used to wrap in (a facet of) the other basic set.
1664 * if so, replace the pair by their union.
1666 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1668 enum isl_change change = isl_change_none;
1670 change = can_wrap_in_set(i, j, info);
1671 if (change != isl_change_none)
1672 return change;
1674 change = can_wrap_in_set(j, i, info);
1675 return change;
1678 /* At least one of the basic maps has an equality that is adjacent
1679 * to inequality. Make sure that only one of the basic maps has
1680 * such an equality and that the other basic map has exactly one
1681 * inequality adjacent to an equality.
1682 * If the other basic map does not have such an inequality, then
1683 * check if all its constraints are either valid or cut constraints
1684 * and, if so, try wrapping in the first map into the second.
1686 * We call the basic map that has the inequality "i" and the basic
1687 * map that has the equality "j".
1688 * If "i" has any "cut" (in)equality, then relaxing the inequality
1689 * by one would not result in a basic map that contains the other
1690 * basic map. However, it may still be possible to wrap in the other
1691 * basic map.
1693 static enum isl_change check_adj_eq(int i, int j,
1694 struct isl_coalesce_info *info)
1696 enum isl_change change = isl_change_none;
1697 int k;
1698 int any_cut;
1700 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1701 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1702 /* ADJ EQ TOO MANY */
1703 return isl_change_none;
1705 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1706 return check_adj_eq(j, i, info);
1708 /* j has an equality adjacent to an inequality in i */
1710 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1711 if (all_valid_or_cut(&info[i]))
1712 return can_wrap_in_set(i, j, info);
1713 return isl_change_none;
1715 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1716 return isl_change_none;
1717 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1718 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1719 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1720 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1721 /* ADJ EQ TOO MANY */
1722 return isl_change_none;
1724 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1725 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1726 break;
1728 if (!any_cut) {
1729 change = is_adj_eq_extension(i, j, k, info);
1730 if (change != isl_change_none)
1731 return change;
1734 change = can_wrap_in_facet(i, j, k, info, any_cut);
1736 return change;
1739 /* The two basic maps lie on adjacent hyperplanes. In particular,
1740 * basic map "i" has an equality that lies parallel to basic map "j".
1741 * Check if we can wrap the facets around the parallel hyperplanes
1742 * to include the other set.
1744 * We perform basically the same operations as can_wrap_in_facet,
1745 * except that we don't need to select a facet of one of the sets.
1747 * \\ \\
1748 * \\ => \\
1749 * \ \|
1751 * If there is more than one equality of "i" adjacent to an equality of "j",
1752 * then the result will satisfy one or more equalities that are a linear
1753 * combination of these equalities. These will be encoded as pairs
1754 * of inequalities in the wrapping constraints and need to be made
1755 * explicit.
1757 static enum isl_change check_eq_adj_eq(int i, int j,
1758 struct isl_coalesce_info *info)
1760 int k;
1761 enum isl_change change = isl_change_none;
1762 int detect_equalities = 0;
1763 struct isl_wraps wraps;
1764 isl_ctx *ctx;
1765 isl_mat *mat;
1766 struct isl_set *set_i = NULL;
1767 struct isl_set *set_j = NULL;
1768 struct isl_vec *bound = NULL;
1769 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1771 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1772 detect_equalities = 1;
1774 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1775 if (info[i].eq[k] == STATUS_ADJ_EQ)
1776 break;
1778 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1779 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1780 ctx = isl_basic_map_get_ctx(info[i].bmap);
1781 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1782 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1783 1 + total);
1784 wraps_init(&wraps, mat, info, i, j);
1785 bound = isl_vec_alloc(ctx, 1 + total);
1786 if (!set_i || !set_j || !wraps.mat || !bound)
1787 goto error;
1789 if (k % 2 == 0)
1790 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1791 else
1792 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1793 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1795 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1796 wraps.mat->n_row = 1;
1798 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1799 goto error;
1800 if (!wraps.mat->n_row)
1801 goto unbounded;
1803 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1804 isl_seq_neg(bound->el, bound->el, 1 + total);
1806 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1807 wraps.mat->n_row++;
1809 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1810 goto error;
1811 if (!wraps.mat->n_row)
1812 goto unbounded;
1814 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1816 if (0) {
1817 error: change = isl_change_error;
1819 unbounded:
1821 wraps_free(&wraps);
1822 isl_set_free(set_i);
1823 isl_set_free(set_j);
1824 isl_vec_free(bound);
1826 return change;
1829 /* Initialize the "eq" and "ineq" fields of "info".
1831 static void init_status(struct isl_coalesce_info *info)
1833 info->eq = info->ineq = NULL;
1836 /* Set info->eq to the positions of the equalities of info->bmap
1837 * with respect to the basic map represented by "tab".
1838 * If info->eq has already been computed, then do not compute it again.
1840 static void set_eq_status_in(struct isl_coalesce_info *info,
1841 struct isl_tab *tab)
1843 if (info->eq)
1844 return;
1845 info->eq = eq_status_in(info->bmap, tab);
1848 /* Set info->ineq to the positions of the inequalities of info->bmap
1849 * with respect to the basic map represented by "tab".
1850 * If info->ineq has already been computed, then do not compute it again.
1852 static void set_ineq_status_in(struct isl_coalesce_info *info,
1853 struct isl_tab *tab)
1855 if (info->ineq)
1856 return;
1857 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1860 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1861 * This function assumes that init_status has been called on "info" first,
1862 * after which the "eq" and "ineq" fields may or may not have been
1863 * assigned a newly allocated array.
1865 static void clear_status(struct isl_coalesce_info *info)
1867 free(info->eq);
1868 free(info->ineq);
1871 /* Check if the union of the given pair of basic maps
1872 * can be represented by a single basic map.
1873 * If so, replace the pair by the single basic map and return
1874 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1875 * Otherwise, return isl_change_none.
1876 * The two basic maps are assumed to live in the same local space.
1877 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1878 * to have been initialized by the caller, either to NULL or
1879 * to valid information.
1881 * We first check the effect of each constraint of one basic map
1882 * on the other basic map.
1883 * The constraint may be
1884 * redundant the constraint is redundant in its own
1885 * basic map and should be ignore and removed
1886 * in the end
1887 * valid all (integer) points of the other basic map
1888 * satisfy the constraint
1889 * separate no (integer) point of the other basic map
1890 * satisfies the constraint
1891 * cut some but not all points of the other basic map
1892 * satisfy the constraint
1893 * adj_eq the given constraint is adjacent (on the outside)
1894 * to an equality of the other basic map
1895 * adj_ineq the given constraint is adjacent (on the outside)
1896 * to an inequality of the other basic map
1898 * We consider seven cases in which we can replace the pair by a single
1899 * basic map. We ignore all "redundant" constraints.
1901 * 1. all constraints of one basic map are valid
1902 * => the other basic map is a subset and can be removed
1904 * 2. all constraints of both basic maps are either "valid" or "cut"
1905 * and the facets corresponding to the "cut" constraints
1906 * of one of the basic maps lies entirely inside the other basic map
1907 * => the pair can be replaced by a basic map consisting
1908 * of the valid constraints in both basic maps
1910 * 3. there is a single pair of adjacent inequalities
1911 * (all other constraints are "valid")
1912 * => the pair can be replaced by a basic map consisting
1913 * of the valid constraints in both basic maps
1915 * 4. one basic map has a single adjacent inequality, while the other
1916 * constraints are "valid". The other basic map has some
1917 * "cut" constraints, but replacing the adjacent inequality by
1918 * its opposite and adding the valid constraints of the other
1919 * basic map results in a subset of the other basic map
1920 * => the pair can be replaced by a basic map consisting
1921 * of the valid constraints in both basic maps
1923 * 5. there is a single adjacent pair of an inequality and an equality,
1924 * the other constraints of the basic map containing the inequality are
1925 * "valid". Moreover, if the inequality the basic map is relaxed
1926 * and then turned into an equality, then resulting facet lies
1927 * entirely inside the other basic map
1928 * => the pair can be replaced by the basic map containing
1929 * the inequality, with the inequality relaxed.
1931 * 6. there is a single adjacent pair of an inequality and an equality,
1932 * the other constraints of the basic map containing the inequality are
1933 * "valid". Moreover, the facets corresponding to both
1934 * the inequality and the equality can be wrapped around their
1935 * ridges to include the other basic map
1936 * => the pair can be replaced by a basic map consisting
1937 * of the valid constraints in both basic maps together
1938 * with all wrapping constraints
1940 * 7. one of the basic maps extends beyond the other by at most one.
1941 * Moreover, the facets corresponding to the cut constraints and
1942 * the pieces of the other basic map at offset one from these cut
1943 * constraints can be wrapped around their ridges to include
1944 * the union of the two basic maps
1945 * => the pair can be replaced by a basic map consisting
1946 * of the valid constraints in both basic maps together
1947 * with all wrapping constraints
1949 * 8. the two basic maps live in adjacent hyperplanes. In principle
1950 * such sets can always be combined through wrapping, but we impose
1951 * that there is only one such pair, to avoid overeager coalescing.
1953 * Throughout the computation, we maintain a collection of tableaus
1954 * corresponding to the basic maps. When the basic maps are dropped
1955 * or combined, the tableaus are modified accordingly.
1957 static enum isl_change coalesce_local_pair_reuse(int i, int j,
1958 struct isl_coalesce_info *info)
1960 enum isl_change change = isl_change_none;
1962 set_eq_status_in(&info[i], info[j].tab);
1963 if (info[i].bmap->n_eq && !info[i].eq)
1964 goto error;
1965 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1966 goto error;
1967 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1968 goto done;
1970 set_eq_status_in(&info[j], info[i].tab);
1971 if (info[j].bmap->n_eq && !info[j].eq)
1972 goto error;
1973 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1974 goto error;
1975 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1976 goto done;
1978 set_ineq_status_in(&info[i], info[j].tab);
1979 if (info[i].bmap->n_ineq && !info[i].ineq)
1980 goto error;
1981 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1982 goto error;
1983 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1984 goto done;
1986 set_ineq_status_in(&info[j], info[i].tab);
1987 if (info[j].bmap->n_ineq && !info[j].ineq)
1988 goto error;
1989 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
1990 goto error;
1991 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
1992 goto done;
1994 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
1995 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
1996 drop(&info[j]);
1997 change = isl_change_drop_second;
1998 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
1999 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2000 drop(&info[i]);
2001 change = isl_change_drop_first;
2002 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2003 change = check_eq_adj_eq(i, j, info);
2004 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2005 change = check_eq_adj_eq(j, i, info);
2006 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2007 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2008 change = check_adj_eq(i, j, info);
2009 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2010 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2011 /* Can't happen */
2012 /* BAD ADJ INEQ */
2013 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2014 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2015 change = check_adj_ineq(i, j, info);
2016 } else {
2017 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2018 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2019 change = check_facets(i, j, info);
2020 if (change == isl_change_none)
2021 change = check_wrap(i, j, info);
2024 done:
2025 clear_status(&info[i]);
2026 clear_status(&info[j]);
2027 return change;
2028 error:
2029 clear_status(&info[i]);
2030 clear_status(&info[j]);
2031 return isl_change_error;
2034 /* Check if the union of the given pair of basic maps
2035 * can be represented by a single basic map.
2036 * If so, replace the pair by the single basic map and return
2037 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2038 * Otherwise, return isl_change_none.
2039 * The two basic maps are assumed to live in the same local space.
2041 static enum isl_change coalesce_local_pair(int i, int j,
2042 struct isl_coalesce_info *info)
2044 init_status(&info[i]);
2045 init_status(&info[j]);
2046 return coalesce_local_pair_reuse(i, j, info);
2049 /* Shift the integer division at position "div" of the basic map
2050 * represented by "info" by "shift".
2052 * That is, if the integer division has the form
2054 * floor(f(x)/d)
2056 * then replace it by
2058 * floor((f(x) + shift * d)/d) - shift
2060 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
2062 unsigned total;
2064 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2065 if (!info->bmap)
2066 return -1;
2068 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2069 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2070 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2071 return -1;
2073 return 0;
2076 /* Check if some of the divs in the basic map represented by "info1"
2077 * are shifts of the corresponding divs in the basic map represented
2078 * by "info2". If so, align them with those of "info2".
2079 * Only do this if "info1" and "info2" have the same number
2080 * of integer divisions.
2082 * An integer division is considered to be a shift of another integer
2083 * division if one is equal to the other plus a constant.
2085 * In particular, for each pair of integer divisions, if both are known,
2086 * have identical coefficients (apart from the constant term) and
2087 * if the difference between the constant terms (taking into account
2088 * the denominator) is an integer, then move the difference outside.
2089 * That is, if one integer division is of the form
2091 * floor((f(x) + c_1)/d)
2093 * while the other is of the form
2095 * floor((f(x) + c_2)/d)
2097 * and n = (c_2 - c_1)/d is an integer, then replace the first
2098 * integer division by
2100 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2102 static int harmonize_divs(struct isl_coalesce_info *info1,
2103 struct isl_coalesce_info *info2)
2105 int i;
2106 int total;
2108 if (!info1->bmap || !info2->bmap)
2109 return -1;
2111 if (info1->bmap->n_div != info2->bmap->n_div)
2112 return 0;
2113 if (info1->bmap->n_div == 0)
2114 return 0;
2116 total = isl_basic_map_total_dim(info1->bmap);
2117 for (i = 0; i < info1->bmap->n_div; ++i) {
2118 isl_int d;
2119 int r = 0;
2121 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
2122 isl_int_is_zero(info2->bmap->div[i][0]))
2123 continue;
2124 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2125 continue;
2126 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
2127 continue;
2128 if (!isl_seq_eq(info1->bmap->div[i] + 2,
2129 info2->bmap->div[i] + 2, total))
2130 continue;
2131 isl_int_init(d);
2132 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
2133 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
2134 isl_int_divexact(d, d, info1->bmap->div[i][0]);
2135 r = shift_div(info1, i, d);
2137 isl_int_clear(d);
2138 if (r < 0)
2139 return -1;
2142 return 0;
2145 /* Do the two basic maps live in the same local space, i.e.,
2146 * do they have the same (known) divs?
2147 * If either basic map has any unknown divs, then we can only assume
2148 * that they do not live in the same local space.
2150 static int same_divs(__isl_keep isl_basic_map *bmap1,
2151 __isl_keep isl_basic_map *bmap2)
2153 int i;
2154 int known;
2155 int total;
2157 if (!bmap1 || !bmap2)
2158 return -1;
2159 if (bmap1->n_div != bmap2->n_div)
2160 return 0;
2162 if (bmap1->n_div == 0)
2163 return 1;
2165 known = isl_basic_map_divs_known(bmap1);
2166 if (known < 0 || !known)
2167 return known;
2168 known = isl_basic_map_divs_known(bmap2);
2169 if (known < 0 || !known)
2170 return known;
2172 total = isl_basic_map_total_dim(bmap1);
2173 for (i = 0; i < bmap1->n_div; ++i)
2174 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2175 return 0;
2177 return 1;
2180 /* Expand info->tab in the same way info->bmap was expanded in
2181 * isl_basic_map_expand_divs using the expansion "exp" and
2182 * update info->ineq with respect to the redundant constraints
2183 * in the resulting tableau.
2184 * In particular, introduce extra variables corresponding
2185 * to the extra integer divisions and add the div constraints
2186 * that were added to info->bmap after info->tab was created
2187 * from the original info->bmap.
2188 * info->ineq was computed without a tableau and therefore
2189 * does not take into account the redundant constraints
2190 * in the tableau. Mark them here.
2192 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp)
2194 unsigned total, pos, n_div;
2195 int extra_var;
2196 int i, n, j, n_ineq;
2197 unsigned n_eq;
2199 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2200 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2201 pos = total - n_div;
2202 extra_var = total - info->tab->n_var;
2203 n = n_div - extra_var;
2205 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2206 return isl_stat_error;
2207 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2208 return isl_stat_error;
2210 i = 0;
2211 for (j = 0; j < n_div; ++j) {
2212 if (i < n && exp[i] == j) {
2213 ++i;
2214 continue;
2216 if (isl_tab_insert_var(info->tab, pos + j) < 0)
2217 return isl_stat_error;
2220 n_ineq = info->tab->n_con - info->tab->n_eq;
2221 for (i = n_ineq; i < info->bmap->n_ineq; ++i)
2222 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2223 return isl_stat_error;
2225 n_eq = info->bmap->n_eq;
2226 for (i = 0; i < info->bmap->n_ineq; ++i) {
2227 if (isl_tab_is_redundant(info->tab, n_eq + i))
2228 info->ineq[i] = STATUS_REDUNDANT;
2231 return isl_stat_ok;
2234 /* Check if the union of the basic maps represented by info[i] and info[j]
2235 * can be represented by a single basic map,
2236 * after expanding the divs of info[i] to match those of info[j].
2237 * If so, replace the pair by the single basic map and return
2238 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2239 * Otherwise, return isl_change_none.
2241 * The caller has already checked for info[j] being a subset of info[i].
2242 * If some of the divs of info[j] are unknown, then the expanded info[i]
2243 * will not have the corresponding div constraints. The other patterns
2244 * therefore cannot apply. Skip the computation in this case.
2246 * The expansion is performed using the divs "div" and expansion "exp"
2247 * computed by the caller.
2248 * info[i].bmap has already been expanded and the result is passed in
2249 * as "bmap".
2250 * The "eq" and "ineq" fields of info[i] reflect the status of
2251 * the constraints of the expanded "bmap" with respect to info[j].tab.
2252 * However, inequality constraints that are redundant in info[i].tab
2253 * have not yet been marked as such because no tableau was available.
2255 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2256 * updating info[i].ineq with respect to the redundant constraints.
2257 * Then try and coalesce the expanded info[i] with info[j],
2258 * reusing the information in info[i].eq and info[i].ineq.
2259 * If this does not result in any coalescing or if it results in info[j]
2260 * getting dropped (which should not happen in practice, since the case
2261 * of info[j] being a subset of info[i] has already been checked by
2262 * the caller), then revert info[i] to its original state.
2264 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2265 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2266 int *exp)
2268 isl_bool known;
2269 isl_basic_map *bmap_i;
2270 struct isl_tab_undo *snap;
2271 enum isl_change change = isl_change_none;
2273 known = isl_basic_map_divs_known(info[j].bmap);
2274 if (known < 0 || !known) {
2275 clear_status(&info[i]);
2276 isl_basic_map_free(bmap);
2277 return known < 0 ? isl_change_error : isl_change_none;
2280 bmap_i = info[i].bmap;
2281 info[i].bmap = isl_basic_map_copy(bmap);
2282 snap = isl_tab_snap(info[i].tab);
2283 if (!info[i].bmap || expand_tab(&info[i], exp) < 0)
2284 change = isl_change_error;
2286 init_status(&info[j]);
2287 if (change == isl_change_none)
2288 change = coalesce_local_pair_reuse(i, j, info);
2289 else
2290 clear_status(&info[i]);
2291 if (change != isl_change_none && change != isl_change_drop_second) {
2292 isl_basic_map_free(bmap_i);
2293 } else {
2294 isl_basic_map_free(info[i].bmap);
2295 info[i].bmap = bmap_i;
2297 if (isl_tab_rollback(info[i].tab, snap) < 0)
2298 change = isl_change_error;
2301 isl_basic_map_free(bmap);
2302 return change;
2305 /* Check if the union of "bmap" and the basic map represented by info[j]
2306 * can be represented by a single basic map,
2307 * after expanding the divs of "bmap" to match those of info[j].
2308 * If so, replace the pair by the single basic map and return
2309 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2310 * Otherwise, return isl_change_none.
2312 * In particular, check if the expanded "bmap" contains the basic map
2313 * represented by the tableau info[j].tab.
2314 * The expansion is performed using the divs "div" and expansion "exp"
2315 * computed by the caller.
2316 * Then we check if all constraints of the expanded "bmap" are valid for
2317 * info[j].tab.
2319 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2320 * In this case, the positions of the constraints of info[i].bmap
2321 * with respect to the basic map represented by info[j] are stored
2322 * in info[i].
2324 * If the expanded "bmap" does not contain the basic map
2325 * represented by the tableau info[j].tab and if "i" is not -1,
2326 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2327 * as well and check if that results in coalescing.
2329 static enum isl_change coalesce_with_expanded_divs(
2330 __isl_keep isl_basic_map *bmap, int i, int j,
2331 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2333 enum isl_change change = isl_change_none;
2334 struct isl_coalesce_info info_local, *info_i;
2336 info_i = i >= 0 ? &info[i] : &info_local;
2337 init_status(info_i);
2338 bmap = isl_basic_map_copy(bmap);
2339 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2341 if (!bmap)
2342 goto error;
2344 info_i->eq = eq_status_in(bmap, info[j].tab);
2345 if (bmap->n_eq && !info_i->eq)
2346 goto error;
2347 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2348 goto error;
2349 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2350 goto done;
2352 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2353 if (bmap->n_ineq && !info_i->ineq)
2354 goto error;
2355 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2356 goto error;
2357 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2358 goto done;
2360 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2361 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2362 drop(&info[j]);
2363 change = isl_change_drop_second;
2366 if (change == isl_change_none && i != -1)
2367 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2369 done:
2370 isl_basic_map_free(bmap);
2371 clear_status(info_i);
2372 return change;
2373 error:
2374 isl_basic_map_free(bmap);
2375 clear_status(info_i);
2376 return isl_change_error;
2379 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2380 * can be represented by a single basic map,
2381 * after aligning the divs of "bmap_i" to match those of info[j].
2382 * If so, replace the pair by the single basic map and return
2383 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2384 * Otherwise, return isl_change_none.
2386 * In particular, check if "bmap_i" contains the basic map represented by
2387 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2388 * Note that this can only succeed if the number of divs of "bmap_i"
2389 * is smaller than (or equal to) the number of divs of info[j].
2391 * We first check if the divs of "bmap_i" are all known and form a subset
2392 * of those of info[j].bmap. If so, we pass control over to
2393 * coalesce_with_expanded_divs.
2395 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2397 static enum isl_change coalesce_after_aligning_divs(
2398 __isl_keep isl_basic_map *bmap_i, int i, int j,
2399 struct isl_coalesce_info *info)
2401 int known;
2402 isl_mat *div_i, *div_j, *div;
2403 int *exp1 = NULL;
2404 int *exp2 = NULL;
2405 isl_ctx *ctx;
2406 enum isl_change change;
2408 known = isl_basic_map_divs_known(bmap_i);
2409 if (known < 0 || !known)
2410 return known;
2412 ctx = isl_basic_map_get_ctx(bmap_i);
2414 div_i = isl_basic_map_get_divs(bmap_i);
2415 div_j = isl_basic_map_get_divs(info[j].bmap);
2417 if (!div_i || !div_j)
2418 goto error;
2420 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2421 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2422 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2423 goto error;
2425 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2426 if (!div)
2427 goto error;
2429 if (div->n_row == div_j->n_row)
2430 change = coalesce_with_expanded_divs(bmap_i,
2431 i, j, info, div, exp1);
2432 else
2433 change = isl_change_none;
2435 isl_mat_free(div);
2437 isl_mat_free(div_i);
2438 isl_mat_free(div_j);
2440 free(exp2);
2441 free(exp1);
2443 return change;
2444 error:
2445 isl_mat_free(div_i);
2446 isl_mat_free(div_j);
2447 free(exp1);
2448 free(exp2);
2449 return isl_change_error;
2452 /* Check if basic map "j" is a subset of basic map "i" after
2453 * exploiting the extra equalities of "j" to simplify the divs of "i".
2454 * If so, remove basic map "j" and return isl_change_drop_second.
2456 * If "j" does not have any equalities or if they are the same
2457 * as those of "i", then we cannot exploit them to simplify the divs.
2458 * Similarly, if there are no divs in "i", then they cannot be simplified.
2459 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2460 * then "j" cannot be a subset of "i".
2462 * Otherwise, we intersect "i" with the affine hull of "j" and then
2463 * check if "j" is a subset of the result after aligning the divs.
2464 * If so, then "j" is definitely a subset of "i" and can be removed.
2465 * Note that if after intersection with the affine hull of "j".
2466 * "i" still has more divs than "j", then there is no way we can
2467 * align the divs of "i" to those of "j".
2469 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2470 struct isl_coalesce_info *info)
2472 isl_basic_map *hull_i, *hull_j, *bmap_i;
2473 int equal, empty;
2474 enum isl_change change;
2476 if (info[j].bmap->n_eq == 0)
2477 return isl_change_none;
2478 if (info[i].bmap->n_div == 0)
2479 return isl_change_none;
2481 hull_i = isl_basic_map_copy(info[i].bmap);
2482 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2483 hull_j = isl_basic_map_copy(info[j].bmap);
2484 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2486 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2487 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2488 empty = isl_basic_map_plain_is_empty(hull_j);
2489 isl_basic_map_free(hull_i);
2491 if (equal < 0 || equal || empty < 0 || empty) {
2492 isl_basic_map_free(hull_j);
2493 if (equal < 0 || empty < 0)
2494 return isl_change_error;
2495 return isl_change_none;
2498 bmap_i = isl_basic_map_copy(info[i].bmap);
2499 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2500 if (!bmap_i)
2501 return isl_change_error;
2503 if (bmap_i->n_div > info[j].bmap->n_div) {
2504 isl_basic_map_free(bmap_i);
2505 return isl_change_none;
2508 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2510 isl_basic_map_free(bmap_i);
2512 return change;
2515 /* Check if the union of and the basic maps represented by info[i] and info[j]
2516 * can be represented by a single basic map, by aligning or equating
2517 * their integer divisions.
2518 * If so, replace the pair by the single basic map and return
2519 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2520 * Otherwise, return isl_change_none.
2522 * Note that we only perform any test if the number of divs is different
2523 * in the two basic maps. In case the number of divs is the same,
2524 * we have already established that the divs are different
2525 * in the two basic maps.
2526 * In particular, if the number of divs of basic map i is smaller than
2527 * the number of divs of basic map j, then we check if j is a subset of i
2528 * and vice versa.
2530 static enum isl_change coalesce_divs(int i, int j,
2531 struct isl_coalesce_info *info)
2533 enum isl_change change = isl_change_none;
2535 if (info[i].bmap->n_div < info[j].bmap->n_div)
2536 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2537 if (change != isl_change_none)
2538 return change;
2540 if (info[j].bmap->n_div < info[i].bmap->n_div)
2541 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2542 if (change != isl_change_none)
2543 return invert_change(change);
2545 change = coalesce_subset_with_equalities(i, j, info);
2546 if (change != isl_change_none)
2547 return change;
2549 change = coalesce_subset_with_equalities(j, i, info);
2550 if (change != isl_change_none)
2551 return invert_change(change);
2553 return isl_change_none;
2556 /* Does "bmap" involve any divs that themselves refer to divs?
2558 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2560 int i;
2561 unsigned total;
2562 unsigned n_div;
2564 total = isl_basic_map_dim(bmap, isl_dim_all);
2565 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2566 total -= n_div;
2568 for (i = 0; i < n_div; ++i)
2569 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2570 n_div) != -1)
2571 return 1;
2573 return 0;
2576 /* Return a list of affine expressions, one for each integer division
2577 * in "bmap_i". For each integer division that also appears in "bmap_j",
2578 * the affine expression is set to NaN. The number of NaNs in the list
2579 * is equal to the number of integer divisions in "bmap_j".
2580 * For the other integer divisions of "bmap_i", the corresponding
2581 * element in the list is a purely affine expression equal to the integer
2582 * division in "hull".
2583 * If no such list can be constructed, then the number of elements
2584 * in the returned list is smaller than the number of integer divisions
2585 * in "bmap_i".
2587 static __isl_give isl_aff_list *set_up_substitutions(
2588 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2589 __isl_take isl_basic_map *hull)
2591 unsigned n_div_i, n_div_j, total;
2592 isl_ctx *ctx;
2593 isl_local_space *ls;
2594 isl_basic_set *wrap_hull;
2595 isl_aff *aff_nan;
2596 isl_aff_list *list;
2597 int i, j;
2599 if (!hull)
2600 return NULL;
2602 ctx = isl_basic_map_get_ctx(hull);
2604 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2605 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2606 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2608 ls = isl_basic_map_get_local_space(bmap_i);
2609 ls = isl_local_space_wrap(ls);
2610 wrap_hull = isl_basic_map_wrap(hull);
2612 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2613 list = isl_aff_list_alloc(ctx, n_div_i);
2615 j = 0;
2616 for (i = 0; i < n_div_i; ++i) {
2617 isl_aff *aff;
2619 if (j < n_div_j &&
2620 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2621 ++j;
2622 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2623 continue;
2625 if (n_div_i - i <= n_div_j - j)
2626 break;
2628 aff = isl_local_space_get_div(ls, i);
2629 aff = isl_aff_substitute_equalities(aff,
2630 isl_basic_set_copy(wrap_hull));
2631 aff = isl_aff_floor(aff);
2632 if (!aff)
2633 goto error;
2634 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2635 isl_aff_free(aff);
2636 break;
2639 list = isl_aff_list_add(list, aff);
2642 isl_aff_free(aff_nan);
2643 isl_local_space_free(ls);
2644 isl_basic_set_free(wrap_hull);
2646 return list;
2647 error:
2648 isl_aff_free(aff_nan);
2649 isl_local_space_free(ls);
2650 isl_basic_set_free(wrap_hull);
2651 isl_aff_list_free(list);
2652 return NULL;
2655 /* Add variables to info->bmap and info->tab corresponding to the elements
2656 * in "list" that are not set to NaN.
2657 * "extra_var" is the number of these elements.
2658 * "dim" is the offset in the variables of "tab" where we should
2659 * start considering the elements in "list".
2660 * When this function returns, the total number of variables in "tab"
2661 * is equal to "dim" plus the number of elements in "list".
2663 * The newly added existentially quantified variables are not given
2664 * an explicit representation because the corresponding div constraints
2665 * do not appear in info->bmap. These constraints are not added
2666 * to info->bmap because for internal consistency, they would need to
2667 * be added to info->tab as well, where they could combine with the equality
2668 * that is added later to result in constraints that do not hold
2669 * in the original input.
2671 static int add_sub_vars(struct isl_coalesce_info *info,
2672 __isl_keep isl_aff_list *list, int dim, int extra_var)
2674 int i, j, n, d;
2675 isl_space *space;
2677 space = isl_basic_map_get_space(info->bmap);
2678 info->bmap = isl_basic_map_cow(info->bmap);
2679 info->bmap = isl_basic_map_extend_space(info->bmap, space,
2680 extra_var, 0, 0);
2681 if (!info->bmap)
2682 return -1;
2683 n = isl_aff_list_n_aff(list);
2684 for (i = 0; i < n; ++i) {
2685 int is_nan;
2686 isl_aff *aff;
2688 aff = isl_aff_list_get_aff(list, i);
2689 is_nan = isl_aff_is_nan(aff);
2690 isl_aff_free(aff);
2691 if (is_nan < 0)
2692 return -1;
2693 if (is_nan)
2694 continue;
2696 if (isl_tab_insert_var(info->tab, dim + i) < 0)
2697 return -1;
2698 d = isl_basic_map_alloc_div(info->bmap);
2699 if (d < 0)
2700 return -1;
2701 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
2702 if (!info->bmap)
2703 return -1;
2704 for (j = d; j > i; --j)
2705 isl_basic_map_swap_div(info->bmap, j - 1, j);
2708 return 0;
2711 /* For each element in "list" that is not set to NaN, fix the corresponding
2712 * variable in "tab" to the purely affine expression defined by the element.
2713 * "dim" is the offset in the variables of "tab" where we should
2714 * start considering the elements in "list".
2716 * This function assumes that a sufficient number of rows and
2717 * elements in the constraint array are available in the tableau.
2719 static int add_sub_equalities(struct isl_tab *tab,
2720 __isl_keep isl_aff_list *list, int dim)
2722 int i, n;
2723 isl_ctx *ctx;
2724 isl_vec *sub;
2725 isl_aff *aff;
2727 n = isl_aff_list_n_aff(list);
2729 ctx = isl_tab_get_ctx(tab);
2730 sub = isl_vec_alloc(ctx, 1 + dim + n);
2731 if (!sub)
2732 return -1;
2733 isl_seq_clr(sub->el + 1 + dim, n);
2735 for (i = 0; i < n; ++i) {
2736 aff = isl_aff_list_get_aff(list, i);
2737 if (!aff)
2738 goto error;
2739 if (isl_aff_is_nan(aff)) {
2740 isl_aff_free(aff);
2741 continue;
2743 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2744 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2745 if (isl_tab_add_eq(tab, sub->el) < 0)
2746 goto error;
2747 isl_int_set_si(sub->el[1 + dim + i], 0);
2748 isl_aff_free(aff);
2751 isl_vec_free(sub);
2752 return 0;
2753 error:
2754 isl_aff_free(aff);
2755 isl_vec_free(sub);
2756 return -1;
2759 /* Add variables to info->tab and info->bmap corresponding to the elements
2760 * in "list" that are not set to NaN. The value of the added variable
2761 * in info->tab is fixed to the purely affine expression defined by the element.
2762 * "dim" is the offset in the variables of info->tab where we should
2763 * start considering the elements in "list".
2764 * When this function returns, the total number of variables in info->tab
2765 * is equal to "dim" plus the number of elements in "list".
2767 static int add_subs(struct isl_coalesce_info *info,
2768 __isl_keep isl_aff_list *list, int dim)
2770 int extra_var;
2771 int n;
2773 if (!list)
2774 return -1;
2776 n = isl_aff_list_n_aff(list);
2777 extra_var = n - (info->tab->n_var - dim);
2779 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2780 return -1;
2781 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2782 return -1;
2783 if (add_sub_vars(info, list, dim, extra_var) < 0)
2784 return -1;
2786 return add_sub_equalities(info->tab, list, dim);
2789 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2790 * divisions in "i" but not in "j" to basic map "j", with values
2791 * specified by "list". The total number of elements in "list"
2792 * is equal to the number of integer divisions in "i", while the number
2793 * of NaN elements in the list is equal to the number of integer divisions
2794 * in "j".
2796 * If no coalescing can be performed, then we need to revert basic map "j"
2797 * to its original state. We do the same if basic map "i" gets dropped
2798 * during the coalescing, even though this should not happen in practice
2799 * since we have already checked for "j" being a subset of "i"
2800 * before we reach this stage.
2802 static enum isl_change coalesce_with_subs(int i, int j,
2803 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2805 isl_basic_map *bmap_j;
2806 struct isl_tab_undo *snap;
2807 unsigned dim;
2808 enum isl_change change;
2810 bmap_j = isl_basic_map_copy(info[j].bmap);
2811 snap = isl_tab_snap(info[j].tab);
2813 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2814 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2815 if (add_subs(&info[j], list, dim) < 0)
2816 goto error;
2818 change = coalesce_local_pair(i, j, info);
2819 if (change != isl_change_none && change != isl_change_drop_first) {
2820 isl_basic_map_free(bmap_j);
2821 } else {
2822 isl_basic_map_free(info[j].bmap);
2823 info[j].bmap = bmap_j;
2825 if (isl_tab_rollback(info[j].tab, snap) < 0)
2826 return isl_change_error;
2829 return change;
2830 error:
2831 isl_basic_map_free(bmap_j);
2832 return isl_change_error;
2835 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2836 * those extra integer divisions in "i" that can be simplified away
2837 * using the extra equalities in "j".
2838 * All divs are assumed to be known and not contain any nested divs.
2840 * We first check if there are any extra equalities in "j" that we
2841 * can exploit. Then we check if every integer division in "i"
2842 * either already appears in "j" or can be simplified using the
2843 * extra equalities to a purely affine expression.
2844 * If these tests succeed, then we try to coalesce the two basic maps
2845 * by introducing extra dimensions in "j" corresponding to
2846 * the extra integer divsisions "i" fixed to the corresponding
2847 * purely affine expression.
2849 static enum isl_change check_coalesce_into_eq(int i, int j,
2850 struct isl_coalesce_info *info)
2852 unsigned n_div_i, n_div_j;
2853 isl_basic_map *hull_i, *hull_j;
2854 int equal, empty;
2855 isl_aff_list *list;
2856 enum isl_change change;
2858 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2859 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2860 if (n_div_i <= n_div_j)
2861 return isl_change_none;
2862 if (info[j].bmap->n_eq == 0)
2863 return isl_change_none;
2865 hull_i = isl_basic_map_copy(info[i].bmap);
2866 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2867 hull_j = isl_basic_map_copy(info[j].bmap);
2868 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2870 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2871 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2872 empty = isl_basic_map_plain_is_empty(hull_j);
2873 isl_basic_map_free(hull_i);
2875 if (equal < 0 || empty < 0)
2876 goto error;
2877 if (equal || empty) {
2878 isl_basic_map_free(hull_j);
2879 return isl_change_none;
2882 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2883 if (!list)
2884 return isl_change_error;
2885 if (isl_aff_list_n_aff(list) < n_div_i)
2886 change = isl_change_none;
2887 else
2888 change = coalesce_with_subs(i, j, info, list);
2890 isl_aff_list_free(list);
2892 return change;
2893 error:
2894 isl_basic_map_free(hull_j);
2895 return isl_change_error;
2898 /* Check if we can coalesce basic maps "i" and "j" after copying
2899 * those extra integer divisions in one of the basic maps that can
2900 * be simplified away using the extra equalities in the other basic map.
2901 * We require all divs to be known in both basic maps.
2902 * Furthermore, to simplify the comparison of div expressions,
2903 * we do not allow any nested integer divisions.
2905 static enum isl_change check_coalesce_eq(int i, int j,
2906 struct isl_coalesce_info *info)
2908 int known, nested;
2909 enum isl_change change;
2911 known = isl_basic_map_divs_known(info[i].bmap);
2912 if (known < 0 || !known)
2913 return known < 0 ? isl_change_error : isl_change_none;
2914 known = isl_basic_map_divs_known(info[j].bmap);
2915 if (known < 0 || !known)
2916 return known < 0 ? isl_change_error : isl_change_none;
2917 nested = has_nested_div(info[i].bmap);
2918 if (nested < 0 || nested)
2919 return nested < 0 ? isl_change_error : isl_change_none;
2920 nested = has_nested_div(info[j].bmap);
2921 if (nested < 0 || nested)
2922 return nested < 0 ? isl_change_error : isl_change_none;
2924 change = check_coalesce_into_eq(i, j, info);
2925 if (change != isl_change_none)
2926 return change;
2927 change = check_coalesce_into_eq(j, i, info);
2928 if (change != isl_change_none)
2929 return invert_change(change);
2931 return isl_change_none;
2934 /* Check if the union of the given pair of basic maps
2935 * can be represented by a single basic map.
2936 * If so, replace the pair by the single basic map and return
2937 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2938 * Otherwise, return isl_change_none.
2940 * We first check if the two basic maps live in the same local space,
2941 * after aligning the divs that differ by only an integer constant.
2942 * If so, we do the complete check. Otherwise, we check if they have
2943 * the same number of integer divisions and can be coalesced, if one is
2944 * an obvious subset of the other or if the extra integer divisions
2945 * of one basic map can be simplified away using the extra equalities
2946 * of the other basic map.
2948 static enum isl_change coalesce_pair(int i, int j,
2949 struct isl_coalesce_info *info)
2951 int same;
2952 enum isl_change change;
2954 if (harmonize_divs(&info[i], &info[j]) < 0)
2955 return isl_change_error;
2956 same = same_divs(info[i].bmap, info[j].bmap);
2957 if (same < 0)
2958 return isl_change_error;
2959 if (same)
2960 return coalesce_local_pair(i, j, info);
2962 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2963 change = coalesce_local_pair(i, j, info);
2964 if (change != isl_change_none)
2965 return change;
2968 change = coalesce_divs(i, j, info);
2969 if (change != isl_change_none)
2970 return change;
2972 return check_coalesce_eq(i, j, info);
2975 /* Return the maximum of "a" and "b".
2977 static int isl_max(int a, int b)
2979 return a > b ? a : b;
2982 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
2983 * with those in the range [start2, end2[, skipping basic maps
2984 * that have been removed (either before or within this function).
2986 * For each basic map i in the first range, we check if it can be coalesced
2987 * with respect to any previously considered basic map j in the second range.
2988 * If i gets dropped (because it was a subset of some j), then
2989 * we can move on to the next basic map.
2990 * If j gets dropped, we need to continue checking against the other
2991 * previously considered basic maps.
2992 * If the two basic maps got fused, then we recheck the fused basic map
2993 * against the previously considered basic maps, starting at i + 1
2994 * (even if start2 is greater than i + 1).
2996 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
2997 int start1, int end1, int start2, int end2)
2999 int i, j;
3001 for (i = end1 - 1; i >= start1; --i) {
3002 if (info[i].removed)
3003 continue;
3004 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3005 enum isl_change changed;
3007 if (info[j].removed)
3008 continue;
3009 if (info[i].removed)
3010 isl_die(ctx, isl_error_internal,
3011 "basic map unexpectedly removed",
3012 return -1);
3013 changed = coalesce_pair(i, j, info);
3014 switch (changed) {
3015 case isl_change_error:
3016 return -1;
3017 case isl_change_none:
3018 case isl_change_drop_second:
3019 continue;
3020 case isl_change_drop_first:
3021 j = end2;
3022 break;
3023 case isl_change_fuse:
3024 j = i;
3025 break;
3030 return 0;
3033 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3035 * We consider groups of basic maps that live in the same apparent
3036 * affine hull and we first coalesce within such a group before we
3037 * coalesce the elements in the group with elements of previously
3038 * considered groups. If a fuse happens during the second phase,
3039 * then we also reconsider the elements within the group.
3041 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3043 int start, end;
3045 for (end = n; end > 0; end = start) {
3046 start = end - 1;
3047 while (start >= 1 &&
3048 info[start - 1].hull_hash == info[start].hull_hash)
3049 start--;
3050 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3051 return -1;
3052 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3053 return -1;
3056 return 0;
3059 /* Update the basic maps in "map" based on the information in "info".
3060 * In particular, remove the basic maps that have been marked removed and
3061 * update the others based on the information in the corresponding tableau.
3062 * Since we detected implicit equalities without calling
3063 * isl_basic_map_gauss, we need to do it now.
3064 * Also call isl_basic_map_simplify if we may have lost the definition
3065 * of one or more integer divisions.
3067 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3068 int n, struct isl_coalesce_info *info)
3070 int i;
3072 if (!map)
3073 return NULL;
3075 for (i = n - 1; i >= 0; --i) {
3076 if (info[i].removed) {
3077 isl_basic_map_free(map->p[i]);
3078 if (i != map->n - 1)
3079 map->p[i] = map->p[map->n - 1];
3080 map->n--;
3081 continue;
3084 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3085 info[i].tab);
3086 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3087 if (info[i].simplify)
3088 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3089 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3090 if (!info[i].bmap)
3091 return isl_map_free(map);
3092 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3093 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3094 isl_basic_map_free(map->p[i]);
3095 map->p[i] = info[i].bmap;
3096 info[i].bmap = NULL;
3099 return map;
3102 /* For each pair of basic maps in the map, check if the union of the two
3103 * can be represented by a single basic map.
3104 * If so, replace the pair by the single basic map and start over.
3106 * We factor out any (hidden) common factor from the constraint
3107 * coefficients to improve the detection of adjacent constraints.
3109 * Since we are constructing the tableaus of the basic maps anyway,
3110 * we exploit them to detect implicit equalities and redundant constraints.
3111 * This also helps the coalescing as it can ignore the redundant constraints.
3112 * In order to avoid confusion, we make all implicit equalities explicit
3113 * in the basic maps. We don't call isl_basic_map_gauss, though,
3114 * as that may affect the number of constraints.
3115 * This means that we have to call isl_basic_map_gauss at the end
3116 * of the computation (in update_basic_maps) to ensure that
3117 * the basic maps are not left in an unexpected state.
3118 * For each basic map, we also compute the hash of the apparent affine hull
3119 * for use in coalesce.
3121 struct isl_map *isl_map_coalesce(struct isl_map *map)
3123 int i;
3124 unsigned n;
3125 isl_ctx *ctx;
3126 struct isl_coalesce_info *info = NULL;
3128 map = isl_map_remove_empty_parts(map);
3129 if (!map)
3130 return NULL;
3132 if (map->n <= 1)
3133 return map;
3135 ctx = isl_map_get_ctx(map);
3136 map = isl_map_sort_divs(map);
3137 map = isl_map_cow(map);
3139 if (!map)
3140 return NULL;
3142 n = map->n;
3144 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3145 if (!info)
3146 goto error;
3148 for (i = 0; i < map->n; ++i) {
3149 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3150 if (!map->p[i])
3151 goto error;
3152 info[i].bmap = isl_basic_map_copy(map->p[i]);
3153 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3154 if (!info[i].tab)
3155 goto error;
3156 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3157 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3158 goto error;
3159 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3160 info[i].bmap);
3161 if (!info[i].bmap)
3162 goto error;
3163 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3164 if (isl_tab_detect_redundant(info[i].tab) < 0)
3165 goto error;
3166 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3167 goto error;
3169 for (i = map->n - 1; i >= 0; --i)
3170 if (info[i].tab->empty)
3171 drop(&info[i]);
3173 if (coalesce(ctx, n, info) < 0)
3174 goto error;
3176 map = update_basic_maps(map, n, info);
3178 clear_coalesce_info(n, info);
3180 return map;
3181 error:
3182 clear_coalesce_info(n, info);
3183 isl_map_free(map);
3184 return NULL;
3187 /* For each pair of basic sets in the set, check if the union of the two
3188 * can be represented by a single basic set.
3189 * If so, replace the pair by the single basic set and start over.
3191 struct isl_set *isl_set_coalesce(struct isl_set *set)
3193 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);