isl_map_coalesce: only tighten non-redundant constraints of potential extension
[isl.git] / isl_coalesce.c
blob437b25fa83b7e51f711d6bc575074cf781683cfb
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 (non-redundant) constraints on the facet represented
852 * by info->tab.
853 * In particular, on input, info->tab represents the result
854 * of replacing constraint k of info->bmap, i.e., f_k >= 0,
855 * by the adjacent equality, i.e., f_k + 1 = 0.
857 * Compute a variable compression from the equality constraint f_k + 1 = 0
858 * and use it to tighten the other constraints of info->bmap,
859 * updating info->tab (and leaving info->bmap untouched).
860 * The compression handles essentially two cases, one where a variable
861 * is assigned a fixed value and can therefore be eliminated, and one
862 * where one variable is a shifted multiple of some other variable and
863 * can therefore be replaced by that multiple.
864 * Gaussian elimination would also work for the first case, but for
865 * the second case, the effectiveness would depend on the order
866 * of the variables.
867 * After compression, some of the constraints may have coefficients
868 * with a common divisor. If this divisor does not divide the constant
869 * term, then the constraint can be tightened.
870 * The tightening is performed on the tableau info->tab by introducing
871 * extra (temporary) constraints.
873 * Only constraints that are possibly affected by the compression are
874 * considered. In particular, if the constraint only involves variables
875 * that are directly mapped to a distinct set of other variables, then
876 * no common divisor can be introduced and no tightening can occur.
878 * It is important to only consider the non-redundant constraints
879 * since the facet constraint has been relaxed prior to the call
880 * to this function, meaning that the constraints that were redundant
881 * prior to the relaxation may no longer be redundant.
882 * These constraints will be ignored in the fused result, so
883 * the fusion detection should not exploit them.
885 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
886 int k)
888 unsigned total;
889 isl_ctx *ctx;
890 isl_vec *v = NULL;
891 isl_mat *T;
892 int i;
893 int *affected;
895 ctx = isl_basic_map_get_ctx(info->bmap);
896 total = isl_basic_map_total_dim(info->bmap);
897 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
898 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
899 T = isl_mat_variable_compression(T, NULL);
900 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
901 if (!T)
902 return isl_stat_error;
903 if (T->n_col == 0) {
904 isl_mat_free(T);
905 return isl_stat_ok;
908 affected = isl_alloc_array(ctx, int, total);
909 if (!affected)
910 goto error;
912 for (i = 0; i < total; ++i)
913 affected[i] = not_unique_unit_row(T, 1 + i);
915 for (i = 0; i < info->bmap->n_ineq; ++i) {
916 if (i == k)
917 continue;
918 if (info->ineq[i] == STATUS_REDUNDANT)
919 continue;
920 if (!is_affected(info->bmap, i, affected, total))
921 continue;
922 v = isl_vec_alloc(ctx, 1 + total);
923 if (!v)
924 goto error;
925 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
926 v = isl_vec_mat_product(v, isl_mat_copy(T));
927 v = try_tightening(info, i, v);
928 isl_vec_free(v);
929 if (!v)
930 goto error;
933 isl_mat_free(T);
934 free(affected);
935 return isl_stat_ok;
936 error:
937 isl_mat_free(T);
938 free(affected);
939 return isl_stat_error;
942 /* Basic map "i" has an inequality "k" that is adjacent to some equality
943 * of basic map "j". All the other inequalities are valid for "j".
944 * Check if basic map "j" forms an extension of basic map "i".
946 * In particular, we relax constraint "k", compute the corresponding
947 * facet and check whether it is included in the other basic map.
948 * Before testing for inclusion, the constraints on the facet
949 * are tightened to increase the chance of an inclusion being detected.
950 * If the facet is included, we know that relaxing the constraint extends
951 * the basic map with exactly the other basic map (we already know that this
952 * other basic map is included in the extension, because there
953 * were no "cut" inequalities in "i") and we can replace the
954 * two basic maps by this extension.
955 * Each integer division that does not have exactly the same
956 * definition in "i" and "j" is marked unknown and the basic map
957 * is scheduled to be simplified in an attempt to recover
958 * the integer division definition.
959 * Place this extension in the position that is the smallest of i and j.
960 * ____ _____
961 * / || / |
962 * / || / |
963 * \ || => \ |
964 * \ || \ |
965 * \___|| \____|
967 static enum isl_change is_adj_eq_extension(int i, int j, int k,
968 struct isl_coalesce_info *info)
970 int change = isl_change_none;
971 int super;
972 struct isl_tab_undo *snap, *snap2;
973 unsigned n_eq = info[i].bmap->n_eq;
975 if (isl_tab_is_equality(info[i].tab, n_eq + k))
976 return isl_change_none;
978 snap = isl_tab_snap(info[i].tab);
979 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
980 return isl_change_error;
981 snap2 = isl_tab_snap(info[i].tab);
982 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
983 return isl_change_error;
984 if (tighten_on_relaxed_facet(&info[i], k) < 0)
985 return isl_change_error;
986 super = contains(&info[j], info[i].tab);
987 if (super < 0)
988 return isl_change_error;
989 if (super) {
990 int l;
991 unsigned total;
993 if (isl_tab_rollback(info[i].tab, snap2) < 0)
994 return isl_change_error;
995 info[i].bmap = isl_basic_map_cow(info[i].bmap);
996 if (!info[i].bmap)
997 return isl_change_error;
998 total = isl_basic_map_total_dim(info[i].bmap);
999 for (l = 0; l < info[i].bmap->n_div; ++l)
1000 if (!isl_seq_eq(info[i].bmap->div[l],
1001 info[j].bmap->div[l], 1 + 1 + total)) {
1002 isl_int_set_si(info[i].bmap->div[l][0], 0);
1003 info[i].simplify = 1;
1005 isl_int_add_ui(info[i].bmap->ineq[k][0],
1006 info[i].bmap->ineq[k][0], 1);
1007 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1008 drop(&info[j]);
1009 if (j < i)
1010 exchange(&info[i], &info[j]);
1011 change = isl_change_fuse;
1012 } else
1013 if (isl_tab_rollback(info[i].tab, snap) < 0)
1014 return isl_change_error;
1016 return change;
1019 /* Data structure that keeps track of the wrapping constraints
1020 * and of information to bound the coefficients of those constraints.
1022 * bound is set if we want to apply a bound on the coefficients
1023 * mat contains the wrapping constraints
1024 * max is the bound on the coefficients (if bound is set)
1026 struct isl_wraps {
1027 int bound;
1028 isl_mat *mat;
1029 isl_int max;
1032 /* Update wraps->max to be greater than or equal to the coefficients
1033 * in the equalities and inequalities of info->bmap that can be removed
1034 * if we end up applying wrapping.
1036 static void wraps_update_max(struct isl_wraps *wraps,
1037 struct isl_coalesce_info *info)
1039 int k;
1040 isl_int max_k;
1041 unsigned total = isl_basic_map_total_dim(info->bmap);
1043 isl_int_init(max_k);
1045 for (k = 0; k < info->bmap->n_eq; ++k) {
1046 if (info->eq[2 * k] == STATUS_VALID &&
1047 info->eq[2 * k + 1] == STATUS_VALID)
1048 continue;
1049 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1050 if (isl_int_abs_gt(max_k, wraps->max))
1051 isl_int_set(wraps->max, max_k);
1054 for (k = 0; k < info->bmap->n_ineq; ++k) {
1055 if (info->ineq[k] == STATUS_VALID ||
1056 info->ineq[k] == STATUS_REDUNDANT)
1057 continue;
1058 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1059 if (isl_int_abs_gt(max_k, wraps->max))
1060 isl_int_set(wraps->max, max_k);
1063 isl_int_clear(max_k);
1066 /* Initialize the isl_wraps data structure.
1067 * If we want to bound the coefficients of the wrapping constraints,
1068 * we set wraps->max to the largest coefficient
1069 * in the equalities and inequalities that can be removed if we end up
1070 * applying wrapping.
1072 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1073 struct isl_coalesce_info *info, int i, int j)
1075 isl_ctx *ctx;
1077 wraps->bound = 0;
1078 wraps->mat = mat;
1079 if (!mat)
1080 return;
1081 ctx = isl_mat_get_ctx(mat);
1082 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1083 if (!wraps->bound)
1084 return;
1085 isl_int_init(wraps->max);
1086 isl_int_set_si(wraps->max, 0);
1087 wraps_update_max(wraps, &info[i]);
1088 wraps_update_max(wraps, &info[j]);
1091 /* Free the contents of the isl_wraps data structure.
1093 static void wraps_free(struct isl_wraps *wraps)
1095 isl_mat_free(wraps->mat);
1096 if (wraps->bound)
1097 isl_int_clear(wraps->max);
1100 /* Is the wrapping constraint in row "row" allowed?
1102 * If wraps->bound is set, we check that none of the coefficients
1103 * is greater than wraps->max.
1105 static int allow_wrap(struct isl_wraps *wraps, int row)
1107 int i;
1109 if (!wraps->bound)
1110 return 1;
1112 for (i = 1; i < wraps->mat->n_col; ++i)
1113 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1114 return 0;
1116 return 1;
1119 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1120 * to include "set" and add the result in position "w" of "wraps".
1121 * "len" is the total number of coefficients in "bound" and "ineq".
1122 * Return 1 on success, 0 on failure and -1 on error.
1123 * Wrapping can fail if the result of wrapping is equal to "bound"
1124 * or if we want to bound the sizes of the coefficients and
1125 * the wrapped constraint does not satisfy this bound.
1127 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1128 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1130 isl_seq_cpy(wraps->mat->row[w], bound, len);
1131 if (negate) {
1132 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1133 ineq = wraps->mat->row[w + 1];
1135 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1136 return -1;
1137 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1138 return 0;
1139 if (!allow_wrap(wraps, w))
1140 return 0;
1141 return 1;
1144 /* For each constraint in info->bmap that is not redundant (as determined
1145 * by info->tab) and that is not a valid constraint for the other basic map,
1146 * wrap the constraint around "bound" such that it includes the whole
1147 * set "set" and append the resulting constraint to "wraps".
1148 * Note that the constraints that are valid for the other basic map
1149 * will be added to the combined basic map by default, so there is
1150 * no need to wrap them.
1151 * The caller wrap_in_facets even relies on this function not wrapping
1152 * any constraints that are already valid.
1153 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1154 * wraps->n_row is the number of actual wrapped constraints that have
1155 * been added.
1156 * If any of the wrapping problems results in a constraint that is
1157 * identical to "bound", then this means that "set" is unbounded in such
1158 * way that no wrapping is possible. If this happens then wraps->n_row
1159 * is reset to zero.
1160 * Similarly, if we want to bound the coefficients of the wrapping
1161 * constraints and a newly added wrapping constraint does not
1162 * satisfy the bound, then wraps->n_row is also reset to zero.
1164 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1165 isl_int *bound, __isl_keep isl_set *set)
1167 int l, m;
1168 int w;
1169 int added;
1170 isl_basic_map *bmap = info->bmap;
1171 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1173 w = wraps->mat->n_row;
1175 for (l = 0; l < bmap->n_ineq; ++l) {
1176 if (info->ineq[l] == STATUS_VALID ||
1177 info->ineq[l] == STATUS_REDUNDANT)
1178 continue;
1179 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1180 continue;
1181 if (isl_seq_eq(bound, bmap->ineq[l], len))
1182 continue;
1183 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1184 continue;
1186 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1187 if (added < 0)
1188 return -1;
1189 if (!added)
1190 goto unbounded;
1191 ++w;
1193 for (l = 0; l < bmap->n_eq; ++l) {
1194 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1195 continue;
1196 if (isl_seq_eq(bound, bmap->eq[l], len))
1197 continue;
1199 for (m = 0; m < 2; ++m) {
1200 if (info->eq[2 * l + m] == STATUS_VALID)
1201 continue;
1202 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1203 set, !m);
1204 if (added < 0)
1205 return -1;
1206 if (!added)
1207 goto unbounded;
1208 ++w;
1212 wraps->mat->n_row = w;
1213 return 0;
1214 unbounded:
1215 wraps->mat->n_row = 0;
1216 return 0;
1219 /* Check if the constraints in "wraps" from "first" until the last
1220 * are all valid for the basic set represented by "tab".
1221 * If not, wraps->n_row is set to zero.
1223 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1224 struct isl_tab *tab)
1226 int i;
1228 for (i = first; i < wraps->n_row; ++i) {
1229 enum isl_ineq_type type;
1230 type = isl_tab_ineq_type(tab, wraps->row[i]);
1231 if (type == isl_ineq_error)
1232 return -1;
1233 if (type == isl_ineq_redundant)
1234 continue;
1235 wraps->n_row = 0;
1236 return 0;
1239 return 0;
1242 /* Return a set that corresponds to the non-redundant constraints
1243 * (as recorded in tab) of bmap.
1245 * It's important to remove the redundant constraints as some
1246 * of the other constraints may have been modified after the
1247 * constraints were marked redundant.
1248 * In particular, a constraint may have been relaxed.
1249 * Redundant constraints are ignored when a constraint is relaxed
1250 * and should therefore continue to be ignored ever after.
1251 * Otherwise, the relaxation might be thwarted by some of
1252 * these constraints.
1254 * Update the underlying set to ensure that the dimension doesn't change.
1255 * Otherwise the integer divisions could get dropped if the tab
1256 * turns out to be empty.
1258 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1259 struct isl_tab *tab)
1261 isl_basic_set *bset;
1263 bmap = isl_basic_map_copy(bmap);
1264 bset = isl_basic_map_underlying_set(bmap);
1265 bset = isl_basic_set_cow(bset);
1266 bset = isl_basic_set_update_from_tab(bset, tab);
1267 return isl_set_from_basic_set(bset);
1270 /* Wrap the constraints of info->bmap that bound the facet defined
1271 * by inequality "k" around (the opposite of) this inequality to
1272 * include "set". "bound" may be used to store the negated inequality.
1273 * Since the wrapped constraints are not guaranteed to contain the whole
1274 * of info->bmap, we check them in check_wraps.
1275 * If any of the wrapped constraints turn out to be invalid, then
1276 * check_wraps will reset wrap->n_row to zero.
1278 static int add_wraps_around_facet(struct isl_wraps *wraps,
1279 struct isl_coalesce_info *info, int k, isl_int *bound,
1280 __isl_keep isl_set *set)
1282 struct isl_tab_undo *snap;
1283 int n;
1284 unsigned total = isl_basic_map_total_dim(info->bmap);
1286 snap = isl_tab_snap(info->tab);
1288 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1289 return -1;
1290 if (isl_tab_detect_redundant(info->tab) < 0)
1291 return -1;
1293 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1295 n = wraps->mat->n_row;
1296 if (add_wraps(wraps, info, bound, set) < 0)
1297 return -1;
1299 if (isl_tab_rollback(info->tab, snap) < 0)
1300 return -1;
1301 if (check_wraps(wraps->mat, n, info->tab) < 0)
1302 return -1;
1304 return 0;
1307 /* Given a basic set i with a constraint k that is adjacent to
1308 * basic set j, check if we can wrap
1309 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1310 * (always) around their ridges to include the other set.
1311 * If so, replace the pair of basic sets by their union.
1313 * All constraints of i (except k) are assumed to be valid or
1314 * cut constraints for j.
1315 * Wrapping the cut constraints to include basic map j may result
1316 * in constraints that are no longer valid of basic map i
1317 * we have to check that the resulting wrapping constraints are valid for i.
1318 * If "wrap_facet" is not set, then all constraints of i (except k)
1319 * are assumed to be valid for j.
1320 * ____ _____
1321 * / | / \
1322 * / || / |
1323 * \ || => \ |
1324 * \ || \ |
1325 * \___|| \____|
1328 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1329 struct isl_coalesce_info *info, int wrap_facet)
1331 enum isl_change change = isl_change_none;
1332 struct isl_wraps wraps;
1333 isl_ctx *ctx;
1334 isl_mat *mat;
1335 struct isl_set *set_i = NULL;
1336 struct isl_set *set_j = NULL;
1337 struct isl_vec *bound = NULL;
1338 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1340 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1341 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1342 ctx = isl_basic_map_get_ctx(info[i].bmap);
1343 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1344 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1345 1 + total);
1346 wraps_init(&wraps, mat, info, i, j);
1347 bound = isl_vec_alloc(ctx, 1 + total);
1348 if (!set_i || !set_j || !wraps.mat || !bound)
1349 goto error;
1351 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1352 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1354 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1355 wraps.mat->n_row = 1;
1357 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1358 goto error;
1359 if (!wraps.mat->n_row)
1360 goto unbounded;
1362 if (wrap_facet) {
1363 if (add_wraps_around_facet(&wraps, &info[i], k,
1364 bound->el, set_j) < 0)
1365 goto error;
1366 if (!wraps.mat->n_row)
1367 goto unbounded;
1370 change = fuse(i, j, info, wraps.mat, 0, 0);
1372 unbounded:
1373 wraps_free(&wraps);
1375 isl_set_free(set_i);
1376 isl_set_free(set_j);
1378 isl_vec_free(bound);
1380 return change;
1381 error:
1382 wraps_free(&wraps);
1383 isl_vec_free(bound);
1384 isl_set_free(set_i);
1385 isl_set_free(set_j);
1386 return isl_change_error;
1389 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1390 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1391 * add wrapping constraints to wrap.mat for all constraints
1392 * of basic map j that bound the part of basic map j that sticks out
1393 * of the cut constraint.
1394 * "set_i" is the underlying set of basic map i.
1395 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1397 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1398 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1399 * (with respect to the integer points), so we add t(x) >= 0 instead.
1400 * Otherwise, we wrap the constraints of basic map j that are not
1401 * redundant in this intersection and that are not already valid
1402 * for basic map i over basic map i.
1403 * Note that it is sufficient to wrap the constraints to include
1404 * basic map i, because we will only wrap the constraints that do
1405 * not include basic map i already. The wrapped constraint will
1406 * therefore be more relaxed compared to the original constraint.
1407 * Since the original constraint is valid for basic map j, so is
1408 * the wrapped constraint.
1410 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1411 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1412 struct isl_tab_undo *snap)
1414 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1415 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1416 return isl_stat_error;
1417 if (isl_tab_detect_redundant(info_j->tab) < 0)
1418 return isl_stat_error;
1420 if (info_j->tab->empty)
1421 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1422 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1423 return isl_stat_error;
1425 if (isl_tab_rollback(info_j->tab, snap) < 0)
1426 return isl_stat_error;
1428 return isl_stat_ok;
1431 /* Given a pair of basic maps i and j such that j sticks out
1432 * of i at n cut constraints, each time by at most one,
1433 * try to compute wrapping constraints and replace the two
1434 * basic maps by a single basic map.
1435 * The other constraints of i are assumed to be valid for j.
1436 * "set_i" is the underlying set of basic map i.
1437 * "wraps" has been initialized to be of the right size.
1439 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1440 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1441 * of basic map j that bound the part of basic map j that sticks out
1442 * of the cut constraint.
1444 * If any wrapping fails, i.e., if we cannot wrap to touch
1445 * the union, then we give up.
1446 * Otherwise, the pair of basic maps is replaced by their union.
1448 static enum isl_change try_wrap_in_facets(int i, int j,
1449 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1450 __isl_keep isl_set *set_i)
1452 int k, l, w;
1453 unsigned total;
1454 struct isl_tab_undo *snap;
1456 total = isl_basic_map_total_dim(info[i].bmap);
1458 snap = isl_tab_snap(info[j].tab);
1460 wraps->mat->n_row = 0;
1462 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1463 for (l = 0; l < 2; ++l) {
1464 if (info[i].eq[2 * k + l] != STATUS_CUT)
1465 continue;
1466 w = wraps->mat->n_row++;
1467 if (l == 0)
1468 isl_seq_neg(wraps->mat->row[w],
1469 info[i].bmap->eq[k], 1 + total);
1470 else
1471 isl_seq_cpy(wraps->mat->row[w],
1472 info[i].bmap->eq[k], 1 + total);
1473 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1474 return isl_change_error;
1476 if (!wraps->mat->n_row)
1477 return isl_change_none;
1481 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1482 if (info[i].ineq[k] != STATUS_CUT)
1483 continue;
1484 w = wraps->mat->n_row++;
1485 isl_seq_cpy(wraps->mat->row[w],
1486 info[i].bmap->ineq[k], 1 + total);
1487 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1488 return isl_change_error;
1490 if (!wraps->mat->n_row)
1491 return isl_change_none;
1494 return fuse(i, j, info, wraps->mat, 0, 1);
1497 /* Given a pair of basic maps i and j such that j sticks out
1498 * of i at n cut constraints, each time by at most one,
1499 * try to compute wrapping constraints and replace the two
1500 * basic maps by a single basic map.
1501 * The other constraints of i are assumed to be valid for j.
1503 * The core computation is performed by try_wrap_in_facets.
1504 * This function simply extracts an underlying set representation
1505 * of basic map i and initializes the data structure for keeping
1506 * track of wrapping constraints.
1508 static enum isl_change wrap_in_facets(int i, int j, int n,
1509 struct isl_coalesce_info *info)
1511 enum isl_change change = isl_change_none;
1512 struct isl_wraps wraps;
1513 isl_ctx *ctx;
1514 isl_mat *mat;
1515 isl_set *set_i = NULL;
1516 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1517 int max_wrap;
1519 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1520 return isl_change_error;
1522 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1523 max_wrap *= n;
1525 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1526 ctx = isl_basic_map_get_ctx(info[i].bmap);
1527 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1528 wraps_init(&wraps, mat, info, i, j);
1529 if (!set_i || !wraps.mat)
1530 goto error;
1532 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1534 wraps_free(&wraps);
1535 isl_set_free(set_i);
1537 return change;
1538 error:
1539 wraps_free(&wraps);
1540 isl_set_free(set_i);
1541 return isl_change_error;
1544 /* Return the effect of inequality "ineq" on the tableau "tab",
1545 * after relaxing the constant term of "ineq" by one.
1547 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1549 enum isl_ineq_type type;
1551 isl_int_add_ui(ineq[0], ineq[0], 1);
1552 type = isl_tab_ineq_type(tab, ineq);
1553 isl_int_sub_ui(ineq[0], ineq[0], 1);
1555 return type;
1558 /* Given two basic sets i and j,
1559 * check if relaxing all the cut constraints of i by one turns
1560 * them into valid constraint for j and check if we can wrap in
1561 * the bits that are sticking out.
1562 * If so, replace the pair by their union.
1564 * We first check if all relaxed cut inequalities of i are valid for j
1565 * and then try to wrap in the intersections of the relaxed cut inequalities
1566 * with j.
1568 * During this wrapping, we consider the points of j that lie at a distance
1569 * of exactly 1 from i. In particular, we ignore the points that lie in
1570 * between this lower-dimensional space and the basic map i.
1571 * We can therefore only apply this to integer maps.
1572 * ____ _____
1573 * / ___|_ / \
1574 * / | | / |
1575 * \ | | => \ |
1576 * \|____| \ |
1577 * \___| \____/
1579 * _____ ______
1580 * | ____|_ | \
1581 * | | | | |
1582 * | | | => | |
1583 * |_| | | |
1584 * |_____| \______|
1586 * _______
1587 * | |
1588 * | |\ |
1589 * | | \ |
1590 * | | \ |
1591 * | | \|
1592 * | | \
1593 * | |_____\
1594 * | |
1595 * |_______|
1597 * Wrapping can fail if the result of wrapping one of the facets
1598 * around its edges does not produce any new facet constraint.
1599 * In particular, this happens when we try to wrap in unbounded sets.
1601 * _______________________________________________________________________
1603 * | ___
1604 * | | |
1605 * |_| |_________________________________________________________________
1606 * |___|
1608 * The following is not an acceptable result of coalescing the above two
1609 * sets as it includes extra integer points.
1610 * _______________________________________________________________________
1612 * |
1613 * |
1615 * \______________________________________________________________________
1617 static enum isl_change can_wrap_in_set(int i, int j,
1618 struct isl_coalesce_info *info)
1620 int k, l;
1621 int n;
1622 unsigned total;
1624 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1625 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1626 return isl_change_none;
1628 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1629 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1630 if (n == 0)
1631 return isl_change_none;
1633 total = isl_basic_map_total_dim(info[i].bmap);
1634 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1635 for (l = 0; l < 2; ++l) {
1636 enum isl_ineq_type type;
1638 if (info[i].eq[2 * k + l] != STATUS_CUT)
1639 continue;
1641 if (l == 0)
1642 isl_seq_neg(info[i].bmap->eq[k],
1643 info[i].bmap->eq[k], 1 + total);
1644 type = type_of_relaxed(info[j].tab,
1645 info[i].bmap->eq[k]);
1646 if (l == 0)
1647 isl_seq_neg(info[i].bmap->eq[k],
1648 info[i].bmap->eq[k], 1 + total);
1649 if (type == isl_ineq_error)
1650 return isl_change_error;
1651 if (type != isl_ineq_redundant)
1652 return isl_change_none;
1656 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1657 enum isl_ineq_type type;
1659 if (info[i].ineq[k] != STATUS_CUT)
1660 continue;
1662 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1663 if (type == isl_ineq_error)
1664 return isl_change_error;
1665 if (type != isl_ineq_redundant)
1666 return isl_change_none;
1669 return wrap_in_facets(i, j, n, info);
1672 /* Check if either i or j has only cut constraints that can
1673 * be used to wrap in (a facet of) the other basic set.
1674 * if so, replace the pair by their union.
1676 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1678 enum isl_change change = isl_change_none;
1680 change = can_wrap_in_set(i, j, info);
1681 if (change != isl_change_none)
1682 return change;
1684 change = can_wrap_in_set(j, i, info);
1685 return change;
1688 /* At least one of the basic maps has an equality that is adjacent
1689 * to inequality. Make sure that only one of the basic maps has
1690 * such an equality and that the other basic map has exactly one
1691 * inequality adjacent to an equality.
1692 * If the other basic map does not have such an inequality, then
1693 * check if all its constraints are either valid or cut constraints
1694 * and, if so, try wrapping in the first map into the second.
1696 * We call the basic map that has the inequality "i" and the basic
1697 * map that has the equality "j".
1698 * If "i" has any "cut" (in)equality, then relaxing the inequality
1699 * by one would not result in a basic map that contains the other
1700 * basic map. However, it may still be possible to wrap in the other
1701 * basic map.
1703 static enum isl_change check_adj_eq(int i, int j,
1704 struct isl_coalesce_info *info)
1706 enum isl_change change = isl_change_none;
1707 int k;
1708 int any_cut;
1710 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1711 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1712 /* ADJ EQ TOO MANY */
1713 return isl_change_none;
1715 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1716 return check_adj_eq(j, i, info);
1718 /* j has an equality adjacent to an inequality in i */
1720 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1721 if (all_valid_or_cut(&info[i]))
1722 return can_wrap_in_set(i, j, info);
1723 return isl_change_none;
1725 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1726 return isl_change_none;
1727 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1728 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1729 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1730 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1731 /* ADJ EQ TOO MANY */
1732 return isl_change_none;
1734 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1735 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1736 break;
1738 if (!any_cut) {
1739 change = is_adj_eq_extension(i, j, k, info);
1740 if (change != isl_change_none)
1741 return change;
1744 change = can_wrap_in_facet(i, j, k, info, any_cut);
1746 return change;
1749 /* The two basic maps lie on adjacent hyperplanes. In particular,
1750 * basic map "i" has an equality that lies parallel to basic map "j".
1751 * Check if we can wrap the facets around the parallel hyperplanes
1752 * to include the other set.
1754 * We perform basically the same operations as can_wrap_in_facet,
1755 * except that we don't need to select a facet of one of the sets.
1757 * \\ \\
1758 * \\ => \\
1759 * \ \|
1761 * If there is more than one equality of "i" adjacent to an equality of "j",
1762 * then the result will satisfy one or more equalities that are a linear
1763 * combination of these equalities. These will be encoded as pairs
1764 * of inequalities in the wrapping constraints and need to be made
1765 * explicit.
1767 static enum isl_change check_eq_adj_eq(int i, int j,
1768 struct isl_coalesce_info *info)
1770 int k;
1771 enum isl_change change = isl_change_none;
1772 int detect_equalities = 0;
1773 struct isl_wraps wraps;
1774 isl_ctx *ctx;
1775 isl_mat *mat;
1776 struct isl_set *set_i = NULL;
1777 struct isl_set *set_j = NULL;
1778 struct isl_vec *bound = NULL;
1779 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1781 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1782 detect_equalities = 1;
1784 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1785 if (info[i].eq[k] == STATUS_ADJ_EQ)
1786 break;
1788 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1789 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1790 ctx = isl_basic_map_get_ctx(info[i].bmap);
1791 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1792 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1793 1 + total);
1794 wraps_init(&wraps, mat, info, i, j);
1795 bound = isl_vec_alloc(ctx, 1 + total);
1796 if (!set_i || !set_j || !wraps.mat || !bound)
1797 goto error;
1799 if (k % 2 == 0)
1800 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1801 else
1802 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1803 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1805 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1806 wraps.mat->n_row = 1;
1808 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1809 goto error;
1810 if (!wraps.mat->n_row)
1811 goto unbounded;
1813 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1814 isl_seq_neg(bound->el, bound->el, 1 + total);
1816 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1817 wraps.mat->n_row++;
1819 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1820 goto error;
1821 if (!wraps.mat->n_row)
1822 goto unbounded;
1824 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1826 if (0) {
1827 error: change = isl_change_error;
1829 unbounded:
1831 wraps_free(&wraps);
1832 isl_set_free(set_i);
1833 isl_set_free(set_j);
1834 isl_vec_free(bound);
1836 return change;
1839 /* Initialize the "eq" and "ineq" fields of "info".
1841 static void init_status(struct isl_coalesce_info *info)
1843 info->eq = info->ineq = NULL;
1846 /* Set info->eq to the positions of the equalities of info->bmap
1847 * with respect to the basic map represented by "tab".
1848 * If info->eq has already been computed, then do not compute it again.
1850 static void set_eq_status_in(struct isl_coalesce_info *info,
1851 struct isl_tab *tab)
1853 if (info->eq)
1854 return;
1855 info->eq = eq_status_in(info->bmap, tab);
1858 /* Set info->ineq to the positions of the inequalities of info->bmap
1859 * with respect to the basic map represented by "tab".
1860 * If info->ineq has already been computed, then do not compute it again.
1862 static void set_ineq_status_in(struct isl_coalesce_info *info,
1863 struct isl_tab *tab)
1865 if (info->ineq)
1866 return;
1867 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1870 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1871 * This function assumes that init_status has been called on "info" first,
1872 * after which the "eq" and "ineq" fields may or may not have been
1873 * assigned a newly allocated array.
1875 static void clear_status(struct isl_coalesce_info *info)
1877 free(info->eq);
1878 free(info->ineq);
1881 /* Check if the union of the given pair of basic maps
1882 * can be represented by a single basic map.
1883 * If so, replace the pair by the single basic map and return
1884 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1885 * Otherwise, return isl_change_none.
1886 * The two basic maps are assumed to live in the same local space.
1887 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1888 * to have been initialized by the caller, either to NULL or
1889 * to valid information.
1891 * We first check the effect of each constraint of one basic map
1892 * on the other basic map.
1893 * The constraint may be
1894 * redundant the constraint is redundant in its own
1895 * basic map and should be ignore and removed
1896 * in the end
1897 * valid all (integer) points of the other basic map
1898 * satisfy the constraint
1899 * separate no (integer) point of the other basic map
1900 * satisfies the constraint
1901 * cut some but not all points of the other basic map
1902 * satisfy the constraint
1903 * adj_eq the given constraint is adjacent (on the outside)
1904 * to an equality of the other basic map
1905 * adj_ineq the given constraint is adjacent (on the outside)
1906 * to an inequality of the other basic map
1908 * We consider seven cases in which we can replace the pair by a single
1909 * basic map. We ignore all "redundant" constraints.
1911 * 1. all constraints of one basic map are valid
1912 * => the other basic map is a subset and can be removed
1914 * 2. all constraints of both basic maps are either "valid" or "cut"
1915 * and the facets corresponding to the "cut" constraints
1916 * of one of the basic maps lies entirely inside the other basic map
1917 * => the pair can be replaced by a basic map consisting
1918 * of the valid constraints in both basic maps
1920 * 3. there is a single pair of adjacent inequalities
1921 * (all other constraints are "valid")
1922 * => the pair can be replaced by a basic map consisting
1923 * of the valid constraints in both basic maps
1925 * 4. one basic map has a single adjacent inequality, while the other
1926 * constraints are "valid". The other basic map has some
1927 * "cut" constraints, but replacing the adjacent inequality by
1928 * its opposite and adding the valid constraints of the other
1929 * basic map results in a subset of the other basic map
1930 * => the pair can be replaced by a basic map consisting
1931 * of the valid constraints in both basic maps
1933 * 5. there is a single adjacent pair of an inequality and an equality,
1934 * the other constraints of the basic map containing the inequality are
1935 * "valid". Moreover, if the inequality the basic map is relaxed
1936 * and then turned into an equality, then resulting facet lies
1937 * entirely inside the other basic map
1938 * => the pair can be replaced by the basic map containing
1939 * the inequality, with the inequality relaxed.
1941 * 6. there is a single adjacent pair of an inequality and an equality,
1942 * the other constraints of the basic map containing the inequality are
1943 * "valid". Moreover, the facets corresponding to both
1944 * the inequality and the equality can be wrapped around their
1945 * ridges to include the other basic map
1946 * => the pair can be replaced by a basic map consisting
1947 * of the valid constraints in both basic maps together
1948 * with all wrapping constraints
1950 * 7. one of the basic maps extends beyond the other by at most one.
1951 * Moreover, the facets corresponding to the cut constraints and
1952 * the pieces of the other basic map at offset one from these cut
1953 * constraints can be wrapped around their ridges to include
1954 * the union of the two basic maps
1955 * => the pair can be replaced by a basic map consisting
1956 * of the valid constraints in both basic maps together
1957 * with all wrapping constraints
1959 * 8. the two basic maps live in adjacent hyperplanes. In principle
1960 * such sets can always be combined through wrapping, but we impose
1961 * that there is only one such pair, to avoid overeager coalescing.
1963 * Throughout the computation, we maintain a collection of tableaus
1964 * corresponding to the basic maps. When the basic maps are dropped
1965 * or combined, the tableaus are modified accordingly.
1967 static enum isl_change coalesce_local_pair_reuse(int i, int j,
1968 struct isl_coalesce_info *info)
1970 enum isl_change change = isl_change_none;
1972 set_eq_status_in(&info[i], info[j].tab);
1973 if (info[i].bmap->n_eq && !info[i].eq)
1974 goto error;
1975 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1976 goto error;
1977 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1978 goto done;
1980 set_eq_status_in(&info[j], info[i].tab);
1981 if (info[j].bmap->n_eq && !info[j].eq)
1982 goto error;
1983 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1984 goto error;
1985 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1986 goto done;
1988 set_ineq_status_in(&info[i], info[j].tab);
1989 if (info[i].bmap->n_ineq && !info[i].ineq)
1990 goto error;
1991 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1992 goto error;
1993 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1994 goto done;
1996 set_ineq_status_in(&info[j], info[i].tab);
1997 if (info[j].bmap->n_ineq && !info[j].ineq)
1998 goto error;
1999 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2000 goto error;
2001 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2002 goto done;
2004 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2005 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2006 drop(&info[j]);
2007 change = isl_change_drop_second;
2008 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2009 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2010 drop(&info[i]);
2011 change = isl_change_drop_first;
2012 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2013 change = check_eq_adj_eq(i, j, info);
2014 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2015 change = check_eq_adj_eq(j, i, info);
2016 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2017 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2018 change = check_adj_eq(i, j, info);
2019 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2020 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2021 /* Can't happen */
2022 /* BAD ADJ INEQ */
2023 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2024 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2025 change = check_adj_ineq(i, j, info);
2026 } else {
2027 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2028 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2029 change = check_facets(i, j, info);
2030 if (change == isl_change_none)
2031 change = check_wrap(i, j, info);
2034 done:
2035 clear_status(&info[i]);
2036 clear_status(&info[j]);
2037 return change;
2038 error:
2039 clear_status(&info[i]);
2040 clear_status(&info[j]);
2041 return isl_change_error;
2044 /* Check if the union of the given pair of basic maps
2045 * can be represented by a single basic map.
2046 * If so, replace the pair by the single basic map and return
2047 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2048 * Otherwise, return isl_change_none.
2049 * The two basic maps are assumed to live in the same local space.
2051 static enum isl_change coalesce_local_pair(int i, int j,
2052 struct isl_coalesce_info *info)
2054 init_status(&info[i]);
2055 init_status(&info[j]);
2056 return coalesce_local_pair_reuse(i, j, info);
2059 /* Shift the integer division at position "div" of the basic map
2060 * represented by "info" by "shift".
2062 * That is, if the integer division has the form
2064 * floor(f(x)/d)
2066 * then replace it by
2068 * floor((f(x) + shift * d)/d) - shift
2070 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
2072 unsigned total;
2074 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2075 if (!info->bmap)
2076 return -1;
2078 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2079 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2080 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2081 return -1;
2083 return 0;
2086 /* Check if some of the divs in the basic map represented by "info1"
2087 * are shifts of the corresponding divs in the basic map represented
2088 * by "info2". If so, align them with those of "info2".
2089 * Only do this if "info1" and "info2" have the same number
2090 * of integer divisions.
2092 * An integer division is considered to be a shift of another integer
2093 * division if one is equal to the other plus a constant.
2095 * In particular, for each pair of integer divisions, if both are known,
2096 * have identical coefficients (apart from the constant term) and
2097 * if the difference between the constant terms (taking into account
2098 * the denominator) is an integer, then move the difference outside.
2099 * That is, if one integer division is of the form
2101 * floor((f(x) + c_1)/d)
2103 * while the other is of the form
2105 * floor((f(x) + c_2)/d)
2107 * and n = (c_2 - c_1)/d is an integer, then replace the first
2108 * integer division by
2110 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2112 static int harmonize_divs(struct isl_coalesce_info *info1,
2113 struct isl_coalesce_info *info2)
2115 int i;
2116 int total;
2118 if (!info1->bmap || !info2->bmap)
2119 return -1;
2121 if (info1->bmap->n_div != info2->bmap->n_div)
2122 return 0;
2123 if (info1->bmap->n_div == 0)
2124 return 0;
2126 total = isl_basic_map_total_dim(info1->bmap);
2127 for (i = 0; i < info1->bmap->n_div; ++i) {
2128 isl_int d;
2129 int r = 0;
2131 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
2132 isl_int_is_zero(info2->bmap->div[i][0]))
2133 continue;
2134 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2135 continue;
2136 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
2137 continue;
2138 if (!isl_seq_eq(info1->bmap->div[i] + 2,
2139 info2->bmap->div[i] + 2, total))
2140 continue;
2141 isl_int_init(d);
2142 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
2143 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
2144 isl_int_divexact(d, d, info1->bmap->div[i][0]);
2145 r = shift_div(info1, i, d);
2147 isl_int_clear(d);
2148 if (r < 0)
2149 return -1;
2152 return 0;
2155 /* Do the two basic maps live in the same local space, i.e.,
2156 * do they have the same (known) divs?
2157 * If either basic map has any unknown divs, then we can only assume
2158 * that they do not live in the same local space.
2160 static int same_divs(__isl_keep isl_basic_map *bmap1,
2161 __isl_keep isl_basic_map *bmap2)
2163 int i;
2164 int known;
2165 int total;
2167 if (!bmap1 || !bmap2)
2168 return -1;
2169 if (bmap1->n_div != bmap2->n_div)
2170 return 0;
2172 if (bmap1->n_div == 0)
2173 return 1;
2175 known = isl_basic_map_divs_known(bmap1);
2176 if (known < 0 || !known)
2177 return known;
2178 known = isl_basic_map_divs_known(bmap2);
2179 if (known < 0 || !known)
2180 return known;
2182 total = isl_basic_map_total_dim(bmap1);
2183 for (i = 0; i < bmap1->n_div; ++i)
2184 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2185 return 0;
2187 return 1;
2190 /* Expand info->tab in the same way info->bmap was expanded in
2191 * isl_basic_map_expand_divs using the expansion "exp" and
2192 * update info->ineq with respect to the redundant constraints
2193 * in the resulting tableau. "bmap" is the original version
2194 * of info->bmap, i.e., the one that corresponds to the current
2195 * state of info->tab. The number of constraints in "bmap"
2196 * is assumed to be the same as the number of constraints
2197 * in info->tab. This is required to be able to detect
2198 * the extra constraints in info->bmap.
2200 * In particular, introduce extra variables corresponding
2201 * to the extra integer divisions and add the div constraints
2202 * that were added to info->bmap after info->tab was created
2203 * from the original info->bmap.
2204 * info->ineq was computed without a tableau and therefore
2205 * does not take into account the redundant constraints
2206 * in the tableau. Mark them here.
2208 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2209 __isl_keep isl_basic_map *bmap)
2211 unsigned total, pos, n_div;
2212 int extra_var;
2213 int i, n, j, n_ineq;
2214 unsigned n_eq;
2216 if (!bmap)
2217 return isl_stat_error;
2218 if (bmap->n_eq + bmap->n_ineq != info->tab->n_con)
2219 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2220 "original tableau does not correspond "
2221 "to original basic map", return isl_stat_error);
2223 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2224 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2225 pos = total - n_div;
2226 extra_var = total - info->tab->n_var;
2227 n = n_div - extra_var;
2229 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2230 return isl_stat_error;
2231 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2232 return isl_stat_error;
2234 i = 0;
2235 for (j = 0; j < n_div; ++j) {
2236 if (i < n && exp[i] == j) {
2237 ++i;
2238 continue;
2240 if (isl_tab_insert_var(info->tab, pos + j) < 0)
2241 return isl_stat_error;
2244 n_ineq = info->tab->n_con - info->tab->n_eq;
2245 for (i = n_ineq; i < info->bmap->n_ineq; ++i)
2246 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2247 return isl_stat_error;
2249 n_eq = info->bmap->n_eq;
2250 for (i = 0; i < info->bmap->n_ineq; ++i) {
2251 if (isl_tab_is_redundant(info->tab, n_eq + i))
2252 info->ineq[i] = STATUS_REDUNDANT;
2255 return isl_stat_ok;
2258 /* Check if the union of the basic maps represented by info[i] and info[j]
2259 * can be represented by a single basic map,
2260 * after expanding the divs of info[i] to match those of info[j].
2261 * If so, replace the pair by the single basic map and return
2262 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2263 * Otherwise, return isl_change_none.
2265 * The caller has already checked for info[j] being a subset of info[i].
2266 * If some of the divs of info[j] are unknown, then the expanded info[i]
2267 * will not have the corresponding div constraints. The other patterns
2268 * therefore cannot apply. Skip the computation in this case.
2270 * The expansion is performed using the divs "div" and expansion "exp"
2271 * computed by the caller.
2272 * info[i].bmap has already been expanded and the result is passed in
2273 * as "bmap".
2274 * The "eq" and "ineq" fields of info[i] reflect the status of
2275 * the constraints of the expanded "bmap" with respect to info[j].tab.
2276 * However, inequality constraints that are redundant in info[i].tab
2277 * have not yet been marked as such because no tableau was available.
2279 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2280 * updating info[i].ineq with respect to the redundant constraints.
2281 * Then try and coalesce the expanded info[i] with info[j],
2282 * reusing the information in info[i].eq and info[i].ineq.
2283 * If this does not result in any coalescing or if it results in info[j]
2284 * getting dropped (which should not happen in practice, since the case
2285 * of info[j] being a subset of info[i] has already been checked by
2286 * the caller), then revert info[i] to its original state.
2288 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2289 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2290 int *exp)
2292 isl_bool known;
2293 isl_basic_map *bmap_i;
2294 struct isl_tab_undo *snap;
2295 enum isl_change change = isl_change_none;
2297 known = isl_basic_map_divs_known(info[j].bmap);
2298 if (known < 0 || !known) {
2299 clear_status(&info[i]);
2300 isl_basic_map_free(bmap);
2301 return known < 0 ? isl_change_error : isl_change_none;
2304 bmap_i = info[i].bmap;
2305 info[i].bmap = isl_basic_map_copy(bmap);
2306 snap = isl_tab_snap(info[i].tab);
2307 if (!info[i].bmap || expand_tab(&info[i], exp, bmap_i) < 0)
2308 change = isl_change_error;
2310 init_status(&info[j]);
2311 if (change == isl_change_none)
2312 change = coalesce_local_pair_reuse(i, j, info);
2313 else
2314 clear_status(&info[i]);
2315 if (change != isl_change_none && change != isl_change_drop_second) {
2316 isl_basic_map_free(bmap_i);
2317 } else {
2318 isl_basic_map_free(info[i].bmap);
2319 info[i].bmap = bmap_i;
2321 if (isl_tab_rollback(info[i].tab, snap) < 0)
2322 change = isl_change_error;
2325 isl_basic_map_free(bmap);
2326 return change;
2329 /* Check if the union of "bmap" and the basic map represented by info[j]
2330 * can be represented by a single basic map,
2331 * after expanding the divs of "bmap" to match those of info[j].
2332 * If so, replace the pair by the single basic map and return
2333 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2334 * Otherwise, return isl_change_none.
2336 * In particular, check if the expanded "bmap" contains the basic map
2337 * represented by the tableau info[j].tab.
2338 * The expansion is performed using the divs "div" and expansion "exp"
2339 * computed by the caller.
2340 * Then we check if all constraints of the expanded "bmap" are valid for
2341 * info[j].tab.
2343 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2344 * In this case, the positions of the constraints of info[i].bmap
2345 * with respect to the basic map represented by info[j] are stored
2346 * in info[i].
2348 * If the expanded "bmap" does not contain the basic map
2349 * represented by the tableau info[j].tab and if "i" is not -1,
2350 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2351 * as well and check if that results in coalescing.
2353 static enum isl_change coalesce_with_expanded_divs(
2354 __isl_keep isl_basic_map *bmap, int i, int j,
2355 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2357 enum isl_change change = isl_change_none;
2358 struct isl_coalesce_info info_local, *info_i;
2360 info_i = i >= 0 ? &info[i] : &info_local;
2361 init_status(info_i);
2362 bmap = isl_basic_map_copy(bmap);
2363 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2365 if (!bmap)
2366 goto error;
2368 info_i->eq = eq_status_in(bmap, info[j].tab);
2369 if (bmap->n_eq && !info_i->eq)
2370 goto error;
2371 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2372 goto error;
2373 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2374 goto done;
2376 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2377 if (bmap->n_ineq && !info_i->ineq)
2378 goto error;
2379 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2380 goto error;
2381 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2382 goto done;
2384 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2385 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2386 drop(&info[j]);
2387 change = isl_change_drop_second;
2390 if (change == isl_change_none && i != -1)
2391 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2393 done:
2394 isl_basic_map_free(bmap);
2395 clear_status(info_i);
2396 return change;
2397 error:
2398 isl_basic_map_free(bmap);
2399 clear_status(info_i);
2400 return isl_change_error;
2403 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2404 * can be represented by a single basic map,
2405 * after aligning the divs of "bmap_i" to match those of info[j].
2406 * If so, replace the pair by the single basic map and return
2407 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2408 * Otherwise, return isl_change_none.
2410 * In particular, check if "bmap_i" contains the basic map represented by
2411 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2412 * Note that this can only succeed if the number of divs of "bmap_i"
2413 * is smaller than (or equal to) the number of divs of info[j].
2415 * We first check if the divs of "bmap_i" are all known and form a subset
2416 * of those of info[j].bmap. If so, we pass control over to
2417 * coalesce_with_expanded_divs.
2419 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2421 static enum isl_change coalesce_after_aligning_divs(
2422 __isl_keep isl_basic_map *bmap_i, int i, int j,
2423 struct isl_coalesce_info *info)
2425 int known;
2426 isl_mat *div_i, *div_j, *div;
2427 int *exp1 = NULL;
2428 int *exp2 = NULL;
2429 isl_ctx *ctx;
2430 enum isl_change change;
2432 known = isl_basic_map_divs_known(bmap_i);
2433 if (known < 0 || !known)
2434 return known;
2436 ctx = isl_basic_map_get_ctx(bmap_i);
2438 div_i = isl_basic_map_get_divs(bmap_i);
2439 div_j = isl_basic_map_get_divs(info[j].bmap);
2441 if (!div_i || !div_j)
2442 goto error;
2444 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2445 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2446 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2447 goto error;
2449 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2450 if (!div)
2451 goto error;
2453 if (div->n_row == div_j->n_row)
2454 change = coalesce_with_expanded_divs(bmap_i,
2455 i, j, info, div, exp1);
2456 else
2457 change = isl_change_none;
2459 isl_mat_free(div);
2461 isl_mat_free(div_i);
2462 isl_mat_free(div_j);
2464 free(exp2);
2465 free(exp1);
2467 return change;
2468 error:
2469 isl_mat_free(div_i);
2470 isl_mat_free(div_j);
2471 free(exp1);
2472 free(exp2);
2473 return isl_change_error;
2476 /* Check if basic map "j" is a subset of basic map "i" after
2477 * exploiting the extra equalities of "j" to simplify the divs of "i".
2478 * If so, remove basic map "j" and return isl_change_drop_second.
2480 * If "j" does not have any equalities or if they are the same
2481 * as those of "i", then we cannot exploit them to simplify the divs.
2482 * Similarly, if there are no divs in "i", then they cannot be simplified.
2483 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2484 * then "j" cannot be a subset of "i".
2486 * Otherwise, we intersect "i" with the affine hull of "j" and then
2487 * check if "j" is a subset of the result after aligning the divs.
2488 * If so, then "j" is definitely a subset of "i" and can be removed.
2489 * Note that if after intersection with the affine hull of "j".
2490 * "i" still has more divs than "j", then there is no way we can
2491 * align the divs of "i" to those of "j".
2493 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2494 struct isl_coalesce_info *info)
2496 isl_basic_map *hull_i, *hull_j, *bmap_i;
2497 int equal, empty;
2498 enum isl_change change;
2500 if (info[j].bmap->n_eq == 0)
2501 return isl_change_none;
2502 if (info[i].bmap->n_div == 0)
2503 return isl_change_none;
2505 hull_i = isl_basic_map_copy(info[i].bmap);
2506 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2507 hull_j = isl_basic_map_copy(info[j].bmap);
2508 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2510 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2511 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2512 empty = isl_basic_map_plain_is_empty(hull_j);
2513 isl_basic_map_free(hull_i);
2515 if (equal < 0 || equal || empty < 0 || empty) {
2516 isl_basic_map_free(hull_j);
2517 if (equal < 0 || empty < 0)
2518 return isl_change_error;
2519 return isl_change_none;
2522 bmap_i = isl_basic_map_copy(info[i].bmap);
2523 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2524 if (!bmap_i)
2525 return isl_change_error;
2527 if (bmap_i->n_div > info[j].bmap->n_div) {
2528 isl_basic_map_free(bmap_i);
2529 return isl_change_none;
2532 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2534 isl_basic_map_free(bmap_i);
2536 return change;
2539 /* Check if the union of and the basic maps represented by info[i] and info[j]
2540 * can be represented by a single basic map, by aligning or equating
2541 * their integer divisions.
2542 * If so, replace the pair by the single basic map and return
2543 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2544 * Otherwise, return isl_change_none.
2546 * Note that we only perform any test if the number of divs is different
2547 * in the two basic maps. In case the number of divs is the same,
2548 * we have already established that the divs are different
2549 * in the two basic maps.
2550 * In particular, if the number of divs of basic map i is smaller than
2551 * the number of divs of basic map j, then we check if j is a subset of i
2552 * and vice versa.
2554 static enum isl_change coalesce_divs(int i, int j,
2555 struct isl_coalesce_info *info)
2557 enum isl_change change = isl_change_none;
2559 if (info[i].bmap->n_div < info[j].bmap->n_div)
2560 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2561 if (change != isl_change_none)
2562 return change;
2564 if (info[j].bmap->n_div < info[i].bmap->n_div)
2565 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2566 if (change != isl_change_none)
2567 return invert_change(change);
2569 change = coalesce_subset_with_equalities(i, j, info);
2570 if (change != isl_change_none)
2571 return change;
2573 change = coalesce_subset_with_equalities(j, i, info);
2574 if (change != isl_change_none)
2575 return invert_change(change);
2577 return isl_change_none;
2580 /* Does "bmap" involve any divs that themselves refer to divs?
2582 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2584 int i;
2585 unsigned total;
2586 unsigned n_div;
2588 total = isl_basic_map_dim(bmap, isl_dim_all);
2589 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2590 total -= n_div;
2592 for (i = 0; i < n_div; ++i)
2593 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2594 n_div) != -1)
2595 return 1;
2597 return 0;
2600 /* Return a list of affine expressions, one for each integer division
2601 * in "bmap_i". For each integer division that also appears in "bmap_j",
2602 * the affine expression is set to NaN. The number of NaNs in the list
2603 * is equal to the number of integer divisions in "bmap_j".
2604 * For the other integer divisions of "bmap_i", the corresponding
2605 * element in the list is a purely affine expression equal to the integer
2606 * division in "hull".
2607 * If no such list can be constructed, then the number of elements
2608 * in the returned list is smaller than the number of integer divisions
2609 * in "bmap_i".
2611 static __isl_give isl_aff_list *set_up_substitutions(
2612 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2613 __isl_take isl_basic_map *hull)
2615 unsigned n_div_i, n_div_j, total;
2616 isl_ctx *ctx;
2617 isl_local_space *ls;
2618 isl_basic_set *wrap_hull;
2619 isl_aff *aff_nan;
2620 isl_aff_list *list;
2621 int i, j;
2623 if (!hull)
2624 return NULL;
2626 ctx = isl_basic_map_get_ctx(hull);
2628 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2629 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2630 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2632 ls = isl_basic_map_get_local_space(bmap_i);
2633 ls = isl_local_space_wrap(ls);
2634 wrap_hull = isl_basic_map_wrap(hull);
2636 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2637 list = isl_aff_list_alloc(ctx, n_div_i);
2639 j = 0;
2640 for (i = 0; i < n_div_i; ++i) {
2641 isl_aff *aff;
2643 if (j < n_div_j &&
2644 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2645 ++j;
2646 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2647 continue;
2649 if (n_div_i - i <= n_div_j - j)
2650 break;
2652 aff = isl_local_space_get_div(ls, i);
2653 aff = isl_aff_substitute_equalities(aff,
2654 isl_basic_set_copy(wrap_hull));
2655 aff = isl_aff_floor(aff);
2656 if (!aff)
2657 goto error;
2658 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2659 isl_aff_free(aff);
2660 break;
2663 list = isl_aff_list_add(list, aff);
2666 isl_aff_free(aff_nan);
2667 isl_local_space_free(ls);
2668 isl_basic_set_free(wrap_hull);
2670 return list;
2671 error:
2672 isl_aff_free(aff_nan);
2673 isl_local_space_free(ls);
2674 isl_basic_set_free(wrap_hull);
2675 isl_aff_list_free(list);
2676 return NULL;
2679 /* Add variables to info->bmap and info->tab corresponding to the elements
2680 * in "list" that are not set to NaN.
2681 * "extra_var" is the number of these elements.
2682 * "dim" is the offset in the variables of "tab" where we should
2683 * start considering the elements in "list".
2684 * When this function returns, the total number of variables in "tab"
2685 * is equal to "dim" plus the number of elements in "list".
2687 * The newly added existentially quantified variables are not given
2688 * an explicit representation because the corresponding div constraints
2689 * do not appear in info->bmap. These constraints are not added
2690 * to info->bmap because for internal consistency, they would need to
2691 * be added to info->tab as well, where they could combine with the equality
2692 * that is added later to result in constraints that do not hold
2693 * in the original input.
2695 static int add_sub_vars(struct isl_coalesce_info *info,
2696 __isl_keep isl_aff_list *list, int dim, int extra_var)
2698 int i, j, n, d;
2699 isl_space *space;
2701 space = isl_basic_map_get_space(info->bmap);
2702 info->bmap = isl_basic_map_cow(info->bmap);
2703 info->bmap = isl_basic_map_extend_space(info->bmap, space,
2704 extra_var, 0, 0);
2705 if (!info->bmap)
2706 return -1;
2707 n = isl_aff_list_n_aff(list);
2708 for (i = 0; i < n; ++i) {
2709 int is_nan;
2710 isl_aff *aff;
2712 aff = isl_aff_list_get_aff(list, i);
2713 is_nan = isl_aff_is_nan(aff);
2714 isl_aff_free(aff);
2715 if (is_nan < 0)
2716 return -1;
2717 if (is_nan)
2718 continue;
2720 if (isl_tab_insert_var(info->tab, dim + i) < 0)
2721 return -1;
2722 d = isl_basic_map_alloc_div(info->bmap);
2723 if (d < 0)
2724 return -1;
2725 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
2726 if (!info->bmap)
2727 return -1;
2728 for (j = d; j > i; --j)
2729 isl_basic_map_swap_div(info->bmap, j - 1, j);
2732 return 0;
2735 /* For each element in "list" that is not set to NaN, fix the corresponding
2736 * variable in "tab" to the purely affine expression defined by the element.
2737 * "dim" is the offset in the variables of "tab" where we should
2738 * start considering the elements in "list".
2740 * This function assumes that a sufficient number of rows and
2741 * elements in the constraint array are available in the tableau.
2743 static int add_sub_equalities(struct isl_tab *tab,
2744 __isl_keep isl_aff_list *list, int dim)
2746 int i, n;
2747 isl_ctx *ctx;
2748 isl_vec *sub;
2749 isl_aff *aff;
2751 n = isl_aff_list_n_aff(list);
2753 ctx = isl_tab_get_ctx(tab);
2754 sub = isl_vec_alloc(ctx, 1 + dim + n);
2755 if (!sub)
2756 return -1;
2757 isl_seq_clr(sub->el + 1 + dim, n);
2759 for (i = 0; i < n; ++i) {
2760 aff = isl_aff_list_get_aff(list, i);
2761 if (!aff)
2762 goto error;
2763 if (isl_aff_is_nan(aff)) {
2764 isl_aff_free(aff);
2765 continue;
2767 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2768 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2769 if (isl_tab_add_eq(tab, sub->el) < 0)
2770 goto error;
2771 isl_int_set_si(sub->el[1 + dim + i], 0);
2772 isl_aff_free(aff);
2775 isl_vec_free(sub);
2776 return 0;
2777 error:
2778 isl_aff_free(aff);
2779 isl_vec_free(sub);
2780 return -1;
2783 /* Add variables to info->tab and info->bmap corresponding to the elements
2784 * in "list" that are not set to NaN. The value of the added variable
2785 * in info->tab is fixed to the purely affine expression defined by the element.
2786 * "dim" is the offset in the variables of info->tab where we should
2787 * start considering the elements in "list".
2788 * When this function returns, the total number of variables in info->tab
2789 * is equal to "dim" plus the number of elements in "list".
2791 static int add_subs(struct isl_coalesce_info *info,
2792 __isl_keep isl_aff_list *list, int dim)
2794 int extra_var;
2795 int n;
2797 if (!list)
2798 return -1;
2800 n = isl_aff_list_n_aff(list);
2801 extra_var = n - (info->tab->n_var - dim);
2803 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2804 return -1;
2805 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2806 return -1;
2807 if (add_sub_vars(info, list, dim, extra_var) < 0)
2808 return -1;
2810 return add_sub_equalities(info->tab, list, dim);
2813 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2814 * divisions in "i" but not in "j" to basic map "j", with values
2815 * specified by "list". The total number of elements in "list"
2816 * is equal to the number of integer divisions in "i", while the number
2817 * of NaN elements in the list is equal to the number of integer divisions
2818 * in "j".
2820 * If no coalescing can be performed, then we need to revert basic map "j"
2821 * to its original state. We do the same if basic map "i" gets dropped
2822 * during the coalescing, even though this should not happen in practice
2823 * since we have already checked for "j" being a subset of "i"
2824 * before we reach this stage.
2826 static enum isl_change coalesce_with_subs(int i, int j,
2827 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2829 isl_basic_map *bmap_j;
2830 struct isl_tab_undo *snap;
2831 unsigned dim;
2832 enum isl_change change;
2834 bmap_j = isl_basic_map_copy(info[j].bmap);
2835 snap = isl_tab_snap(info[j].tab);
2837 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2838 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2839 if (add_subs(&info[j], list, dim) < 0)
2840 goto error;
2842 change = coalesce_local_pair(i, j, info);
2843 if (change != isl_change_none && change != isl_change_drop_first) {
2844 isl_basic_map_free(bmap_j);
2845 } else {
2846 isl_basic_map_free(info[j].bmap);
2847 info[j].bmap = bmap_j;
2849 if (isl_tab_rollback(info[j].tab, snap) < 0)
2850 return isl_change_error;
2853 return change;
2854 error:
2855 isl_basic_map_free(bmap_j);
2856 return isl_change_error;
2859 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2860 * those extra integer divisions in "i" that can be simplified away
2861 * using the extra equalities in "j".
2862 * All divs are assumed to be known and not contain any nested divs.
2864 * We first check if there are any extra equalities in "j" that we
2865 * can exploit. Then we check if every integer division in "i"
2866 * either already appears in "j" or can be simplified using the
2867 * extra equalities to a purely affine expression.
2868 * If these tests succeed, then we try to coalesce the two basic maps
2869 * by introducing extra dimensions in "j" corresponding to
2870 * the extra integer divsisions "i" fixed to the corresponding
2871 * purely affine expression.
2873 static enum isl_change check_coalesce_into_eq(int i, int j,
2874 struct isl_coalesce_info *info)
2876 unsigned n_div_i, n_div_j;
2877 isl_basic_map *hull_i, *hull_j;
2878 int equal, empty;
2879 isl_aff_list *list;
2880 enum isl_change change;
2882 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2883 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2884 if (n_div_i <= n_div_j)
2885 return isl_change_none;
2886 if (info[j].bmap->n_eq == 0)
2887 return isl_change_none;
2889 hull_i = isl_basic_map_copy(info[i].bmap);
2890 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2891 hull_j = isl_basic_map_copy(info[j].bmap);
2892 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2894 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2895 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2896 empty = isl_basic_map_plain_is_empty(hull_j);
2897 isl_basic_map_free(hull_i);
2899 if (equal < 0 || empty < 0)
2900 goto error;
2901 if (equal || empty) {
2902 isl_basic_map_free(hull_j);
2903 return isl_change_none;
2906 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2907 if (!list)
2908 return isl_change_error;
2909 if (isl_aff_list_n_aff(list) < n_div_i)
2910 change = isl_change_none;
2911 else
2912 change = coalesce_with_subs(i, j, info, list);
2914 isl_aff_list_free(list);
2916 return change;
2917 error:
2918 isl_basic_map_free(hull_j);
2919 return isl_change_error;
2922 /* Check if we can coalesce basic maps "i" and "j" after copying
2923 * those extra integer divisions in one of the basic maps that can
2924 * be simplified away using the extra equalities in the other basic map.
2925 * We require all divs to be known in both basic maps.
2926 * Furthermore, to simplify the comparison of div expressions,
2927 * we do not allow any nested integer divisions.
2929 static enum isl_change check_coalesce_eq(int i, int j,
2930 struct isl_coalesce_info *info)
2932 int known, nested;
2933 enum isl_change change;
2935 known = isl_basic_map_divs_known(info[i].bmap);
2936 if (known < 0 || !known)
2937 return known < 0 ? isl_change_error : isl_change_none;
2938 known = isl_basic_map_divs_known(info[j].bmap);
2939 if (known < 0 || !known)
2940 return known < 0 ? isl_change_error : isl_change_none;
2941 nested = has_nested_div(info[i].bmap);
2942 if (nested < 0 || nested)
2943 return nested < 0 ? isl_change_error : isl_change_none;
2944 nested = has_nested_div(info[j].bmap);
2945 if (nested < 0 || nested)
2946 return nested < 0 ? isl_change_error : isl_change_none;
2948 change = check_coalesce_into_eq(i, j, info);
2949 if (change != isl_change_none)
2950 return change;
2951 change = check_coalesce_into_eq(j, i, info);
2952 if (change != isl_change_none)
2953 return invert_change(change);
2955 return isl_change_none;
2958 /* Check if the union of the given pair of basic maps
2959 * can be represented by a single basic map.
2960 * If so, replace the pair by the single basic map and return
2961 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2962 * Otherwise, return isl_change_none.
2964 * We first check if the two basic maps live in the same local space,
2965 * after aligning the divs that differ by only an integer constant.
2966 * If so, we do the complete check. Otherwise, we check if they have
2967 * the same number of integer divisions and can be coalesced, if one is
2968 * an obvious subset of the other or if the extra integer divisions
2969 * of one basic map can be simplified away using the extra equalities
2970 * of the other basic map.
2972 static enum isl_change coalesce_pair(int i, int j,
2973 struct isl_coalesce_info *info)
2975 int same;
2976 enum isl_change change;
2978 if (harmonize_divs(&info[i], &info[j]) < 0)
2979 return isl_change_error;
2980 same = same_divs(info[i].bmap, info[j].bmap);
2981 if (same < 0)
2982 return isl_change_error;
2983 if (same)
2984 return coalesce_local_pair(i, j, info);
2986 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2987 change = coalesce_local_pair(i, j, info);
2988 if (change != isl_change_none)
2989 return change;
2992 change = coalesce_divs(i, j, info);
2993 if (change != isl_change_none)
2994 return change;
2996 return check_coalesce_eq(i, j, info);
2999 /* Return the maximum of "a" and "b".
3001 static int isl_max(int a, int b)
3003 return a > b ? a : b;
3006 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3007 * with those in the range [start2, end2[, skipping basic maps
3008 * that have been removed (either before or within this function).
3010 * For each basic map i in the first range, we check if it can be coalesced
3011 * with respect to any previously considered basic map j in the second range.
3012 * If i gets dropped (because it was a subset of some j), then
3013 * we can move on to the next basic map.
3014 * If j gets dropped, we need to continue checking against the other
3015 * previously considered basic maps.
3016 * If the two basic maps got fused, then we recheck the fused basic map
3017 * against the previously considered basic maps, starting at i + 1
3018 * (even if start2 is greater than i + 1).
3020 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3021 int start1, int end1, int start2, int end2)
3023 int i, j;
3025 for (i = end1 - 1; i >= start1; --i) {
3026 if (info[i].removed)
3027 continue;
3028 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3029 enum isl_change changed;
3031 if (info[j].removed)
3032 continue;
3033 if (info[i].removed)
3034 isl_die(ctx, isl_error_internal,
3035 "basic map unexpectedly removed",
3036 return -1);
3037 changed = coalesce_pair(i, j, info);
3038 switch (changed) {
3039 case isl_change_error:
3040 return -1;
3041 case isl_change_none:
3042 case isl_change_drop_second:
3043 continue;
3044 case isl_change_drop_first:
3045 j = end2;
3046 break;
3047 case isl_change_fuse:
3048 j = i;
3049 break;
3054 return 0;
3057 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3059 * We consider groups of basic maps that live in the same apparent
3060 * affine hull and we first coalesce within such a group before we
3061 * coalesce the elements in the group with elements of previously
3062 * considered groups. If a fuse happens during the second phase,
3063 * then we also reconsider the elements within the group.
3065 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3067 int start, end;
3069 for (end = n; end > 0; end = start) {
3070 start = end - 1;
3071 while (start >= 1 &&
3072 info[start - 1].hull_hash == info[start].hull_hash)
3073 start--;
3074 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3075 return -1;
3076 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3077 return -1;
3080 return 0;
3083 /* Update the basic maps in "map" based on the information in "info".
3084 * In particular, remove the basic maps that have been marked removed and
3085 * update the others based on the information in the corresponding tableau.
3086 * Since we detected implicit equalities without calling
3087 * isl_basic_map_gauss, we need to do it now.
3088 * Also call isl_basic_map_simplify if we may have lost the definition
3089 * of one or more integer divisions.
3091 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3092 int n, struct isl_coalesce_info *info)
3094 int i;
3096 if (!map)
3097 return NULL;
3099 for (i = n - 1; i >= 0; --i) {
3100 if (info[i].removed) {
3101 isl_basic_map_free(map->p[i]);
3102 if (i != map->n - 1)
3103 map->p[i] = map->p[map->n - 1];
3104 map->n--;
3105 continue;
3108 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3109 info[i].tab);
3110 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3111 if (info[i].simplify)
3112 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3113 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3114 if (!info[i].bmap)
3115 return isl_map_free(map);
3116 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3117 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3118 isl_basic_map_free(map->p[i]);
3119 map->p[i] = info[i].bmap;
3120 info[i].bmap = NULL;
3123 return map;
3126 /* For each pair of basic maps in the map, check if the union of the two
3127 * can be represented by a single basic map.
3128 * If so, replace the pair by the single basic map and start over.
3130 * We factor out any (hidden) common factor from the constraint
3131 * coefficients to improve the detection of adjacent constraints.
3133 * Since we are constructing the tableaus of the basic maps anyway,
3134 * we exploit them to detect implicit equalities and redundant constraints.
3135 * This also helps the coalescing as it can ignore the redundant constraints.
3136 * In order to avoid confusion, we make all implicit equalities explicit
3137 * in the basic maps. We don't call isl_basic_map_gauss, though,
3138 * as that may affect the number of constraints.
3139 * This means that we have to call isl_basic_map_gauss at the end
3140 * of the computation (in update_basic_maps) to ensure that
3141 * the basic maps are not left in an unexpected state.
3142 * For each basic map, we also compute the hash of the apparent affine hull
3143 * for use in coalesce.
3145 struct isl_map *isl_map_coalesce(struct isl_map *map)
3147 int i;
3148 unsigned n;
3149 isl_ctx *ctx;
3150 struct isl_coalesce_info *info = NULL;
3152 map = isl_map_remove_empty_parts(map);
3153 if (!map)
3154 return NULL;
3156 if (map->n <= 1)
3157 return map;
3159 ctx = isl_map_get_ctx(map);
3160 map = isl_map_sort_divs(map);
3161 map = isl_map_cow(map);
3163 if (!map)
3164 return NULL;
3166 n = map->n;
3168 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3169 if (!info)
3170 goto error;
3172 for (i = 0; i < map->n; ++i) {
3173 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3174 if (!map->p[i])
3175 goto error;
3176 info[i].bmap = isl_basic_map_copy(map->p[i]);
3177 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3178 if (!info[i].tab)
3179 goto error;
3180 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3181 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3182 goto error;
3183 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3184 info[i].bmap);
3185 if (!info[i].bmap)
3186 goto error;
3187 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3188 if (isl_tab_detect_redundant(info[i].tab) < 0)
3189 goto error;
3190 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3191 goto error;
3193 for (i = map->n - 1; i >= 0; --i)
3194 if (info[i].tab->empty)
3195 drop(&info[i]);
3197 if (coalesce(ctx, n, info) < 0)
3198 goto error;
3200 map = update_basic_maps(map, n, info);
3202 clear_coalesce_info(n, info);
3204 return map;
3205 error:
3206 clear_coalesce_info(n, info);
3207 isl_map_free(map);
3208 return NULL;
3211 /* For each pair of basic sets in the set, check if the union of the two
3212 * can be represented by a single basic set.
3213 * If so, replace the pair by the single basic set and start over.
3215 struct isl_set *isl_set_coalesce(struct isl_set *set)
3217 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);