interface/python.cc: document order of superclasses
[isl.git] / isl_coalesce.c
blob20da8db7a8d28f33822d33776c01eab7bc4deefb
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 #include <set_to_map.c>
31 #include <set_from_map.c>
33 #define STATUS_ERROR -1
34 #define STATUS_REDUNDANT 1
35 #define STATUS_VALID 2
36 #define STATUS_SEPARATE 3
37 #define STATUS_CUT 4
38 #define STATUS_ADJ_EQ 5
39 #define STATUS_ADJ_INEQ 6
41 static int status_in(isl_int *ineq, struct isl_tab *tab)
43 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
44 switch (type) {
45 default:
46 case isl_ineq_error: return STATUS_ERROR;
47 case isl_ineq_redundant: return STATUS_VALID;
48 case isl_ineq_separate: return STATUS_SEPARATE;
49 case isl_ineq_cut: return STATUS_CUT;
50 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
51 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
55 /* Compute the position of the equalities of basic map "bmap_i"
56 * with respect to the basic map represented by "tab_j".
57 * The resulting array has twice as many entries as the number
58 * of equalities corresponding to the two inequalties to which
59 * each equality corresponds.
61 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
62 struct isl_tab *tab_j)
64 int k, l;
65 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
66 unsigned dim;
68 if (!eq)
69 return NULL;
71 dim = isl_basic_map_total_dim(bmap_i);
72 for (k = 0; k < bmap_i->n_eq; ++k) {
73 for (l = 0; l < 2; ++l) {
74 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
75 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
76 if (eq[2 * k + l] == STATUS_ERROR)
77 goto error;
79 if (eq[2 * k] == STATUS_SEPARATE ||
80 eq[2 * k + 1] == STATUS_SEPARATE)
81 break;
84 return eq;
85 error:
86 free(eq);
87 return NULL;
90 /* Compute the position of the inequalities of basic map "bmap_i"
91 * (also represented by "tab_i", if not NULL) with respect to the basic map
92 * represented by "tab_j".
94 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
95 struct isl_tab *tab_i, struct isl_tab *tab_j)
97 int k;
98 unsigned n_eq = bmap_i->n_eq;
99 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
101 if (!ineq)
102 return NULL;
104 for (k = 0; k < bmap_i->n_ineq; ++k) {
105 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
106 ineq[k] = STATUS_REDUNDANT;
107 continue;
109 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
110 if (ineq[k] == STATUS_ERROR)
111 goto error;
112 if (ineq[k] == STATUS_SEPARATE)
113 break;
116 return ineq;
117 error:
118 free(ineq);
119 return NULL;
122 static int any(int *con, unsigned len, int status)
124 int i;
126 for (i = 0; i < len ; ++i)
127 if (con[i] == status)
128 return 1;
129 return 0;
132 static int count(int *con, unsigned len, int status)
134 int i;
135 int c = 0;
137 for (i = 0; i < len ; ++i)
138 if (con[i] == status)
139 c++;
140 return c;
143 static int all(int *con, unsigned len, int status)
145 int i;
147 for (i = 0; i < len ; ++i) {
148 if (con[i] == STATUS_REDUNDANT)
149 continue;
150 if (con[i] != status)
151 return 0;
153 return 1;
156 /* Internal information associated to a basic map in a map
157 * that is to be coalesced by isl_map_coalesce.
159 * "bmap" is the basic map itself (or NULL if "removed" is set)
160 * "tab" is the corresponding tableau (or NULL if "removed" is set)
161 * "hull_hash" identifies the affine space in which "bmap" lives.
162 * "removed" is set if this basic map has been removed from the map
163 * "simplify" is set if this basic map may have some unknown integer
164 * divisions that were not present in the input basic maps. The basic
165 * map should then be simplified such that we may be able to find
166 * a definition among the constraints.
168 * "eq" and "ineq" are only set if we are currently trying to coalesce
169 * this basic map with another basic map, in which case they represent
170 * the position of the inequalities of this basic map with respect to
171 * the other basic map. The number of elements in the "eq" array
172 * is twice the number of equalities in the "bmap", corresponding
173 * to the two inequalities that make up each equality.
175 struct isl_coalesce_info {
176 isl_basic_map *bmap;
177 struct isl_tab *tab;
178 uint32_t hull_hash;
179 int removed;
180 int simplify;
181 int *eq;
182 int *ineq;
185 /* Are all non-redundant constraints of the basic map represented by "info"
186 * either valid or cut constraints with respect to the other basic map?
188 static int all_valid_or_cut(struct isl_coalesce_info *info)
190 int i;
192 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
193 if (info->eq[i] == STATUS_REDUNDANT)
194 continue;
195 if (info->eq[i] == STATUS_VALID)
196 continue;
197 if (info->eq[i] == STATUS_CUT)
198 continue;
199 return 0;
202 for (i = 0; i < info->bmap->n_ineq; ++i) {
203 if (info->ineq[i] == STATUS_REDUNDANT)
204 continue;
205 if (info->ineq[i] == STATUS_VALID)
206 continue;
207 if (info->ineq[i] == STATUS_CUT)
208 continue;
209 return 0;
212 return 1;
215 /* Compute the hash of the (apparent) affine hull of info->bmap (with
216 * the existentially quantified variables removed) and store it
217 * in info->hash.
219 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
221 isl_basic_map *hull;
222 unsigned n_div;
224 hull = isl_basic_map_copy(info->bmap);
225 hull = isl_basic_map_plain_affine_hull(hull);
226 n_div = isl_basic_map_dim(hull, isl_dim_div);
227 hull = isl_basic_map_drop_constraints_involving_dims(hull,
228 isl_dim_div, 0, n_div);
229 info->hull_hash = isl_basic_map_get_hash(hull);
230 isl_basic_map_free(hull);
232 return hull ? 0 : -1;
235 /* Free all the allocated memory in an array
236 * of "n" isl_coalesce_info elements.
238 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
240 int i;
242 if (!info)
243 return;
245 for (i = 0; i < n; ++i) {
246 isl_basic_map_free(info[i].bmap);
247 isl_tab_free(info[i].tab);
250 free(info);
253 /* Drop the basic map represented by "info".
254 * That is, clear the memory associated to the entry and
255 * mark it as having been removed.
257 static void drop(struct isl_coalesce_info *info)
259 info->bmap = isl_basic_map_free(info->bmap);
260 isl_tab_free(info->tab);
261 info->tab = NULL;
262 info->removed = 1;
265 /* Exchange the information in "info1" with that in "info2".
267 static void exchange(struct isl_coalesce_info *info1,
268 struct isl_coalesce_info *info2)
270 struct isl_coalesce_info info;
272 info = *info1;
273 *info1 = *info2;
274 *info2 = info;
277 /* This type represents the kind of change that has been performed
278 * while trying to coalesce two basic maps.
280 * isl_change_none: nothing was changed
281 * isl_change_drop_first: the first basic map was removed
282 * isl_change_drop_second: the second basic map was removed
283 * isl_change_fuse: the two basic maps were replaced by a new basic map.
285 enum isl_change {
286 isl_change_error = -1,
287 isl_change_none = 0,
288 isl_change_drop_first,
289 isl_change_drop_second,
290 isl_change_fuse,
293 /* Update "change" based on an interchange of the first and the second
294 * basic map. That is, interchange isl_change_drop_first and
295 * isl_change_drop_second.
297 static enum isl_change invert_change(enum isl_change change)
299 switch (change) {
300 case isl_change_error:
301 return isl_change_error;
302 case isl_change_none:
303 return isl_change_none;
304 case isl_change_drop_first:
305 return isl_change_drop_second;
306 case isl_change_drop_second:
307 return isl_change_drop_first;
308 case isl_change_fuse:
309 return isl_change_fuse;
312 return isl_change_error;
315 /* Add the valid constraints of the basic map represented by "info"
316 * to "bmap". "len" is the size of the constraints.
317 * If only one of the pair of inequalities that make up an equality
318 * is valid, then add that inequality.
320 static __isl_give isl_basic_map *add_valid_constraints(
321 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
322 unsigned len)
324 int k, l;
326 if (!bmap)
327 return NULL;
329 for (k = 0; k < info->bmap->n_eq; ++k) {
330 if (info->eq[2 * k] == STATUS_VALID &&
331 info->eq[2 * k + 1] == STATUS_VALID) {
332 l = isl_basic_map_alloc_equality(bmap);
333 if (l < 0)
334 return isl_basic_map_free(bmap);
335 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
336 } else if (info->eq[2 * k] == STATUS_VALID) {
337 l = isl_basic_map_alloc_inequality(bmap);
338 if (l < 0)
339 return isl_basic_map_free(bmap);
340 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
341 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
342 l = isl_basic_map_alloc_inequality(bmap);
343 if (l < 0)
344 return isl_basic_map_free(bmap);
345 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
349 for (k = 0; k < info->bmap->n_ineq; ++k) {
350 if (info->ineq[k] != STATUS_VALID)
351 continue;
352 l = isl_basic_map_alloc_inequality(bmap);
353 if (l < 0)
354 return isl_basic_map_free(bmap);
355 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
358 return bmap;
361 /* Is "bmap" defined by a number of (non-redundant) constraints that
362 * is greater than the number of constraints of basic maps i and j combined?
363 * Equalities are counted as two inequalities.
365 static int number_of_constraints_increases(int i, int j,
366 struct isl_coalesce_info *info,
367 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
369 int k, n_old, n_new;
371 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
372 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
374 n_new = 2 * bmap->n_eq;
375 for (k = 0; k < bmap->n_ineq; ++k)
376 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
377 ++n_new;
379 return n_new > n_old;
382 /* Replace the pair of basic maps i and j by the basic map bounded
383 * by the valid constraints in both basic maps and the constraints
384 * in extra (if not NULL).
385 * Place the fused basic map in the position that is the smallest of i and j.
387 * If "detect_equalities" is set, then look for equalities encoded
388 * as pairs of inequalities.
389 * If "check_number" is set, then the original basic maps are only
390 * replaced if the total number of constraints does not increase.
391 * While the number of integer divisions in the two basic maps
392 * is assumed to be the same, the actual definitions may be different.
393 * We only copy the definition from one of the basic map if it is
394 * the same as that of the other basic map. Otherwise, we mark
395 * the integer division as unknown and simplify the basic map
396 * in an attempt to recover the integer division definition.
398 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
399 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
401 int k, l;
402 struct isl_basic_map *fused = NULL;
403 struct isl_tab *fused_tab = NULL;
404 unsigned total = isl_basic_map_total_dim(info[i].bmap);
405 unsigned extra_rows = extra ? extra->n_row : 0;
406 unsigned n_eq, n_ineq;
407 int simplify = 0;
409 if (j < i)
410 return fuse(j, i, info, extra, detect_equalities, check_number);
412 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
413 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
414 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
415 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
416 fused = add_valid_constraints(fused, &info[i], 1 + total);
417 fused = add_valid_constraints(fused, &info[j], 1 + total);
418 if (!fused)
419 goto error;
420 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
421 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
422 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
424 for (k = 0; k < info[i].bmap->n_div; ++k) {
425 int l = isl_basic_map_alloc_div(fused);
426 if (l < 0)
427 goto error;
428 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
429 1 + 1 + total)) {
430 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
431 1 + 1 + total);
432 } else {
433 isl_int_set_si(fused->div[l][0], 0);
434 simplify = 1;
438 for (k = 0; k < extra_rows; ++k) {
439 l = isl_basic_map_alloc_inequality(fused);
440 if (l < 0)
441 goto error;
442 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
445 if (detect_equalities)
446 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
447 fused = isl_basic_map_gauss(fused, NULL);
448 if (simplify || info[j].simplify) {
449 fused = isl_basic_map_simplify(fused);
450 info[i].simplify = 0;
452 fused = isl_basic_map_finalize(fused);
454 fused_tab = isl_tab_from_basic_map(fused, 0);
455 if (isl_tab_detect_redundant(fused_tab) < 0)
456 goto error;
458 if (check_number &&
459 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
460 isl_tab_free(fused_tab);
461 isl_basic_map_free(fused);
462 return isl_change_none;
465 isl_basic_map_free(info[i].bmap);
466 info[i].bmap = fused;
467 isl_tab_free(info[i].tab);
468 info[i].tab = fused_tab;
469 drop(&info[j]);
471 return isl_change_fuse;
472 error:
473 isl_tab_free(fused_tab);
474 isl_basic_map_free(fused);
475 return isl_change_error;
478 /* Given a pair of basic maps i and j such that all constraints are either
479 * "valid" or "cut", check if the facets corresponding to the "cut"
480 * constraints of i lie entirely within basic map j.
481 * If so, replace the pair by the basic map consisting of the valid
482 * constraints in both basic maps.
483 * Checking whether the facet lies entirely within basic map j
484 * is performed by checking whether the constraints of basic map j
485 * are valid for the facet. These tests are performed on a rational
486 * tableau to avoid the theoretical possibility that a constraint
487 * that was considered to be a cut constraint for the entire basic map i
488 * happens to be considered to be a valid constraint for the facet,
489 * even though it cuts off the same rational points.
491 * To see that we are not introducing any extra points, call the
492 * two basic maps A and B and the resulting map U and let x
493 * be an element of U \setminus ( A \cup B ).
494 * A line connecting x with an element of A \cup B meets a facet F
495 * of either A or B. Assume it is a facet of B and let c_1 be
496 * the corresponding facet constraint. We have c_1(x) < 0 and
497 * so c_1 is a cut constraint. This implies that there is some
498 * (possibly rational) point x' satisfying the constraints of A
499 * and the opposite of c_1 as otherwise c_1 would have been marked
500 * valid for A. The line connecting x and x' meets a facet of A
501 * in a (possibly rational) point that also violates c_1, but this
502 * is impossible since all cut constraints of B are valid for all
503 * cut facets of A.
504 * In case F is a facet of A rather than B, then we can apply the
505 * above reasoning to find a facet of B separating x from A \cup B first.
507 static enum isl_change check_facets(int i, int j,
508 struct isl_coalesce_info *info)
510 int k, l;
511 struct isl_tab_undo *snap, *snap2;
512 unsigned n_eq = info[i].bmap->n_eq;
514 snap = isl_tab_snap(info[i].tab);
515 if (isl_tab_mark_rational(info[i].tab) < 0)
516 return isl_change_error;
517 snap2 = isl_tab_snap(info[i].tab);
519 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
520 if (info[i].ineq[k] != STATUS_CUT)
521 continue;
522 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
523 return isl_change_error;
524 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
525 int stat;
526 if (info[j].ineq[l] != STATUS_CUT)
527 continue;
528 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
529 if (stat < 0)
530 return isl_change_error;
531 if (stat != STATUS_VALID)
532 break;
534 if (isl_tab_rollback(info[i].tab, snap2) < 0)
535 return isl_change_error;
536 if (l < info[j].bmap->n_ineq)
537 break;
540 if (k < info[i].bmap->n_ineq) {
541 if (isl_tab_rollback(info[i].tab, snap) < 0)
542 return isl_change_error;
543 return isl_change_none;
545 return fuse(i, j, info, NULL, 0, 0);
548 /* Check if info->bmap contains the basic map represented
549 * by the tableau "tab".
550 * For each equality, we check both the constraint itself
551 * (as an inequality) and its negation. Make sure the
552 * equality is returned to its original state before returning.
554 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
556 int k;
557 unsigned dim;
558 isl_basic_map *bmap = info->bmap;
560 dim = isl_basic_map_total_dim(bmap);
561 for (k = 0; k < bmap->n_eq; ++k) {
562 int stat;
563 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
564 stat = status_in(bmap->eq[k], tab);
565 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
566 if (stat < 0)
567 return -1;
568 if (stat != STATUS_VALID)
569 return 0;
570 stat = status_in(bmap->eq[k], tab);
571 if (stat < 0)
572 return -1;
573 if (stat != STATUS_VALID)
574 return 0;
577 for (k = 0; k < bmap->n_ineq; ++k) {
578 int stat;
579 if (info->ineq[k] == STATUS_REDUNDANT)
580 continue;
581 stat = status_in(bmap->ineq[k], tab);
582 if (stat < 0)
583 return -1;
584 if (stat != STATUS_VALID)
585 return 0;
587 return 1;
590 /* Basic map "i" has an inequality (say "k") that is adjacent
591 * to some inequality of basic map "j". All the other inequalities
592 * are valid for "j".
593 * Check if basic map "j" forms an extension of basic map "i".
595 * Note that this function is only called if some of the equalities or
596 * inequalities of basic map "j" do cut basic map "i". The function is
597 * correct even if there are no such cut constraints, but in that case
598 * the additional checks performed by this function are overkill.
600 * In particular, we replace constraint k, say f >= 0, by constraint
601 * f <= -1, add the inequalities of "j" that are valid for "i"
602 * and check if the result is a subset of basic map "j".
603 * If so, then we know that this result is exactly equal to basic map "j"
604 * since all its constraints are valid for basic map "j".
605 * By combining the valid constraints of "i" (all equalities and all
606 * inequalities except "k") and the valid constraints of "j" we therefore
607 * obtain a basic map that is equal to their union.
608 * In this case, there is no need to perform a rollback of the tableau
609 * since it is going to be destroyed in fuse().
612 * |\__ |\__
613 * | \__ | \__
614 * | \_ => | \__
615 * |_______| _ |_________\
618 * |\ |\
619 * | \ | \
620 * | \ | \
621 * | | | \
622 * | ||\ => | \
623 * | || \ | \
624 * | || | | |
625 * |__||_/ |_____/
627 static enum isl_change is_adj_ineq_extension(int i, int j,
628 struct isl_coalesce_info *info)
630 int k;
631 struct isl_tab_undo *snap;
632 unsigned n_eq = info[i].bmap->n_eq;
633 unsigned total = isl_basic_map_total_dim(info[i].bmap);
634 int r;
635 int super;
637 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
638 return isl_change_error;
640 for (k = 0; k < info[i].bmap->n_ineq; ++k)
641 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
642 break;
643 if (k >= info[i].bmap->n_ineq)
644 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
645 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
646 return isl_change_error);
648 snap = isl_tab_snap(info[i].tab);
650 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
651 return isl_change_error;
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 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
656 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
657 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
658 if (r < 0)
659 return isl_change_error;
661 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
662 if (info[j].ineq[k] != STATUS_VALID)
663 continue;
664 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
665 return isl_change_error;
668 super = contains(&info[j], info[i].tab);
669 if (super < 0)
670 return isl_change_error;
671 if (super)
672 return fuse(i, j, info, NULL, 0, 0);
674 if (isl_tab_rollback(info[i].tab, snap) < 0)
675 return isl_change_error;
677 return isl_change_none;
681 /* Both basic maps have at least one inequality with and adjacent
682 * (but opposite) inequality in the other basic map.
683 * Check that there are no cut constraints and that there is only
684 * a single pair of adjacent inequalities.
685 * If so, we can replace the pair by a single basic map described
686 * by all but the pair of adjacent inequalities.
687 * Any additional points introduced lie strictly between the two
688 * adjacent hyperplanes and can therefore be integral.
690 * ____ _____
691 * / ||\ / \
692 * / || \ / \
693 * \ || \ => \ \
694 * \ || / \ /
695 * \___||_/ \_____/
697 * The test for a single pair of adjancent inequalities is important
698 * for avoiding the combination of two basic maps like the following
700 * /|
701 * / |
702 * /__|
703 * _____
704 * | |
705 * | |
706 * |___|
708 * If there are some cut constraints on one side, then we may
709 * still be able to fuse the two basic maps, but we need to perform
710 * some additional checks in is_adj_ineq_extension.
712 static enum isl_change check_adj_ineq(int i, int j,
713 struct isl_coalesce_info *info)
715 int count_i, count_j;
716 int cut_i, cut_j;
718 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
719 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
721 if (count_i != 1 && count_j != 1)
722 return isl_change_none;
724 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
725 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
726 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
727 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
729 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
730 return fuse(i, j, info, NULL, 0, 0);
732 if (count_i == 1 && !cut_i)
733 return is_adj_ineq_extension(i, j, info);
735 if (count_j == 1 && !cut_j)
736 return is_adj_ineq_extension(j, i, info);
738 return isl_change_none;
741 /* Given an affine transformation matrix "T", does row "row" represent
742 * anything other than a unit vector (possibly shifted by a constant)
743 * that is not involved in any of the other rows?
745 * That is, if a constraint involves the variable corresponding to
746 * the row, then could its preimage by "T" have any coefficients
747 * that are different from those in the original constraint?
749 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
751 int i, j;
752 int len = T->n_col - 1;
754 i = isl_seq_first_non_zero(T->row[row] + 1, len);
755 if (i < 0)
756 return 1;
757 if (!isl_int_is_one(T->row[row][1 + i]) &&
758 !isl_int_is_negone(T->row[row][1 + i]))
759 return 1;
761 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
762 if (j >= 0)
763 return 1;
765 for (j = 1; j < T->n_row; ++j) {
766 if (j == row)
767 continue;
768 if (!isl_int_is_zero(T->row[j][1 + i]))
769 return 1;
772 return 0;
775 /* Does inequality constraint "ineq" of "bmap" involve any of
776 * the variables marked in "affected"?
777 * "total" is the total number of variables, i.e., the number
778 * of entries in "affected".
780 static int is_affected(__isl_keep isl_basic_map *bmap, int ineq, int *affected,
781 int total)
783 int i;
785 for (i = 0; i < total; ++i) {
786 if (!affected[i])
787 continue;
788 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
789 return 1;
792 return 0;
795 /* Given the compressed version of inequality constraint "ineq"
796 * of info->bmap in "v", check if the constraint can be tightened,
797 * where the compression is based on an equality constraint valid
798 * for info->tab.
799 * If so, add the tightened version of the inequality constraint
800 * to info->tab. "v" may be modified by this function.
802 * That is, if the compressed constraint is of the form
804 * m f() + c >= 0
806 * with 0 < c < m, then it is equivalent to
808 * f() >= 0
810 * This means that c can also be subtracted from the original,
811 * uncompressed constraint without affecting the integer points
812 * in info->tab. Add this tightened constraint as an extra row
813 * to info->tab to make this information explicitly available.
815 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
816 int ineq, __isl_take isl_vec *v)
818 isl_ctx *ctx;
819 int r;
821 if (!v)
822 return NULL;
824 ctx = isl_vec_get_ctx(v);
825 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
826 if (isl_int_is_zero(ctx->normalize_gcd) ||
827 isl_int_is_one(ctx->normalize_gcd)) {
828 return v;
831 v = isl_vec_cow(v);
832 if (!v)
833 return NULL;
835 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
836 if (isl_int_is_zero(v->el[0]))
837 return v;
839 if (isl_tab_extend_cons(info->tab, 1) < 0)
840 return isl_vec_free(v);
842 isl_int_sub(info->bmap->ineq[ineq][0],
843 info->bmap->ineq[ineq][0], v->el[0]);
844 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
845 isl_int_add(info->bmap->ineq[ineq][0],
846 info->bmap->ineq[ineq][0], v->el[0]);
848 if (r < 0)
849 return isl_vec_free(v);
851 return v;
854 /* Tighten the (non-redundant) constraints on the facet represented
855 * by info->tab.
856 * In particular, on input, info->tab represents the result
857 * of replacing constraint k of info->bmap, i.e., f_k >= 0,
858 * by the adjacent equality, i.e., f_k + 1 = 0.
860 * Compute a variable compression from the equality constraint f_k + 1 = 0
861 * and use it to tighten the other constraints of info->bmap,
862 * updating info->tab (and leaving info->bmap untouched).
863 * The compression handles essentially two cases, one where a variable
864 * is assigned a fixed value and can therefore be eliminated, and one
865 * where one variable is a shifted multiple of some other variable and
866 * can therefore be replaced by that multiple.
867 * Gaussian elimination would also work for the first case, but for
868 * the second case, the effectiveness would depend on the order
869 * of the variables.
870 * After compression, some of the constraints may have coefficients
871 * with a common divisor. If this divisor does not divide the constant
872 * term, then the constraint can be tightened.
873 * The tightening is performed on the tableau info->tab by introducing
874 * extra (temporary) constraints.
876 * Only constraints that are possibly affected by the compression are
877 * considered. In particular, if the constraint only involves variables
878 * that are directly mapped to a distinct set of other variables, then
879 * no common divisor can be introduced and no tightening can occur.
881 * It is important to only consider the non-redundant constraints
882 * since the facet constraint has been relaxed prior to the call
883 * to this function, meaning that the constraints that were redundant
884 * prior to the relaxation may no longer be redundant.
885 * These constraints will be ignored in the fused result, so
886 * the fusion detection should not exploit them.
888 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
889 int k)
891 unsigned total;
892 isl_ctx *ctx;
893 isl_vec *v = NULL;
894 isl_mat *T;
895 int i;
896 int *affected;
898 ctx = isl_basic_map_get_ctx(info->bmap);
899 total = isl_basic_map_total_dim(info->bmap);
900 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
901 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
902 T = isl_mat_variable_compression(T, NULL);
903 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
904 if (!T)
905 return isl_stat_error;
906 if (T->n_col == 0) {
907 isl_mat_free(T);
908 return isl_stat_ok;
911 affected = isl_alloc_array(ctx, int, total);
912 if (!affected)
913 goto error;
915 for (i = 0; i < total; ++i)
916 affected[i] = not_unique_unit_row(T, 1 + i);
918 for (i = 0; i < info->bmap->n_ineq; ++i) {
919 if (i == k)
920 continue;
921 if (info->ineq[i] == STATUS_REDUNDANT)
922 continue;
923 if (!is_affected(info->bmap, i, affected, total))
924 continue;
925 v = isl_vec_alloc(ctx, 1 + total);
926 if (!v)
927 goto error;
928 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
929 v = isl_vec_mat_product(v, isl_mat_copy(T));
930 v = try_tightening(info, i, v);
931 isl_vec_free(v);
932 if (!v)
933 goto error;
936 isl_mat_free(T);
937 free(affected);
938 return isl_stat_ok;
939 error:
940 isl_mat_free(T);
941 free(affected);
942 return isl_stat_error;
945 /* Basic map "i" has an inequality "k" that is adjacent to some equality
946 * of basic map "j". All the other inequalities are valid for "j".
947 * Check if basic map "j" forms an extension of basic map "i".
949 * In particular, we relax constraint "k", compute the corresponding
950 * facet and check whether it is included in the other basic map.
951 * Before testing for inclusion, the constraints on the facet
952 * are tightened to increase the chance of an inclusion being detected.
953 * If the facet is included, we know that relaxing the constraint extends
954 * the basic map with exactly the other basic map (we already know that this
955 * other basic map is included in the extension, because there
956 * were no "cut" inequalities in "i") and we can replace the
957 * two basic maps by this extension.
958 * Each integer division that does not have exactly the same
959 * definition in "i" and "j" is marked unknown and the basic map
960 * is scheduled to be simplified in an attempt to recover
961 * the integer division definition.
962 * Place this extension in the position that is the smallest of i and j.
963 * ____ _____
964 * / || / |
965 * / || / |
966 * \ || => \ |
967 * \ || \ |
968 * \___|| \____|
970 static enum isl_change is_adj_eq_extension(int i, int j, int k,
971 struct isl_coalesce_info *info)
973 int change = isl_change_none;
974 int super;
975 struct isl_tab_undo *snap, *snap2;
976 unsigned n_eq = info[i].bmap->n_eq;
978 if (isl_tab_is_equality(info[i].tab, n_eq + k))
979 return isl_change_none;
981 snap = isl_tab_snap(info[i].tab);
982 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
983 return isl_change_error;
984 snap2 = isl_tab_snap(info[i].tab);
985 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
986 return isl_change_error;
987 if (tighten_on_relaxed_facet(&info[i], k) < 0)
988 return isl_change_error;
989 super = contains(&info[j], info[i].tab);
990 if (super < 0)
991 return isl_change_error;
992 if (super) {
993 int l;
994 unsigned total;
996 if (isl_tab_rollback(info[i].tab, snap2) < 0)
997 return isl_change_error;
998 info[i].bmap = isl_basic_map_cow(info[i].bmap);
999 if (!info[i].bmap)
1000 return isl_change_error;
1001 total = isl_basic_map_total_dim(info[i].bmap);
1002 for (l = 0; l < info[i].bmap->n_div; ++l)
1003 if (!isl_seq_eq(info[i].bmap->div[l],
1004 info[j].bmap->div[l], 1 + 1 + total)) {
1005 isl_int_set_si(info[i].bmap->div[l][0], 0);
1006 info[i].simplify = 1;
1008 isl_int_add_ui(info[i].bmap->ineq[k][0],
1009 info[i].bmap->ineq[k][0], 1);
1010 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1011 drop(&info[j]);
1012 if (j < i)
1013 exchange(&info[i], &info[j]);
1014 change = isl_change_fuse;
1015 } else
1016 if (isl_tab_rollback(info[i].tab, snap) < 0)
1017 return isl_change_error;
1019 return change;
1022 /* Data structure that keeps track of the wrapping constraints
1023 * and of information to bound the coefficients of those constraints.
1025 * bound is set if we want to apply a bound on the coefficients
1026 * mat contains the wrapping constraints
1027 * max is the bound on the coefficients (if bound is set)
1029 struct isl_wraps {
1030 int bound;
1031 isl_mat *mat;
1032 isl_int max;
1035 /* Update wraps->max to be greater than or equal to the coefficients
1036 * in the equalities and inequalities of info->bmap that can be removed
1037 * if we end up applying wrapping.
1039 static void wraps_update_max(struct isl_wraps *wraps,
1040 struct isl_coalesce_info *info)
1042 int k;
1043 isl_int max_k;
1044 unsigned total = isl_basic_map_total_dim(info->bmap);
1046 isl_int_init(max_k);
1048 for (k = 0; k < info->bmap->n_eq; ++k) {
1049 if (info->eq[2 * k] == STATUS_VALID &&
1050 info->eq[2 * k + 1] == STATUS_VALID)
1051 continue;
1052 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1053 if (isl_int_abs_gt(max_k, wraps->max))
1054 isl_int_set(wraps->max, max_k);
1057 for (k = 0; k < info->bmap->n_ineq; ++k) {
1058 if (info->ineq[k] == STATUS_VALID ||
1059 info->ineq[k] == STATUS_REDUNDANT)
1060 continue;
1061 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1062 if (isl_int_abs_gt(max_k, wraps->max))
1063 isl_int_set(wraps->max, max_k);
1066 isl_int_clear(max_k);
1069 /* Initialize the isl_wraps data structure.
1070 * If we want to bound the coefficients of the wrapping constraints,
1071 * we set wraps->max to the largest coefficient
1072 * in the equalities and inequalities that can be removed if we end up
1073 * applying wrapping.
1075 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1076 struct isl_coalesce_info *info, int i, int j)
1078 isl_ctx *ctx;
1080 wraps->bound = 0;
1081 wraps->mat = mat;
1082 if (!mat)
1083 return;
1084 ctx = isl_mat_get_ctx(mat);
1085 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1086 if (!wraps->bound)
1087 return;
1088 isl_int_init(wraps->max);
1089 isl_int_set_si(wraps->max, 0);
1090 wraps_update_max(wraps, &info[i]);
1091 wraps_update_max(wraps, &info[j]);
1094 /* Free the contents of the isl_wraps data structure.
1096 static void wraps_free(struct isl_wraps *wraps)
1098 isl_mat_free(wraps->mat);
1099 if (wraps->bound)
1100 isl_int_clear(wraps->max);
1103 /* Is the wrapping constraint in row "row" allowed?
1105 * If wraps->bound is set, we check that none of the coefficients
1106 * is greater than wraps->max.
1108 static int allow_wrap(struct isl_wraps *wraps, int row)
1110 int i;
1112 if (!wraps->bound)
1113 return 1;
1115 for (i = 1; i < wraps->mat->n_col; ++i)
1116 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1117 return 0;
1119 return 1;
1122 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1123 * to include "set" and add the result in position "w" of "wraps".
1124 * "len" is the total number of coefficients in "bound" and "ineq".
1125 * Return 1 on success, 0 on failure and -1 on error.
1126 * Wrapping can fail if the result of wrapping is equal to "bound"
1127 * or if we want to bound the sizes of the coefficients and
1128 * the wrapped constraint does not satisfy this bound.
1130 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1131 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1133 isl_seq_cpy(wraps->mat->row[w], bound, len);
1134 if (negate) {
1135 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1136 ineq = wraps->mat->row[w + 1];
1138 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1139 return -1;
1140 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1141 return 0;
1142 if (!allow_wrap(wraps, w))
1143 return 0;
1144 return 1;
1147 /* For each constraint in info->bmap that is not redundant (as determined
1148 * by info->tab) and that is not a valid constraint for the other basic map,
1149 * wrap the constraint around "bound" such that it includes the whole
1150 * set "set" and append the resulting constraint to "wraps".
1151 * Note that the constraints that are valid for the other basic map
1152 * will be added to the combined basic map by default, so there is
1153 * no need to wrap them.
1154 * The caller wrap_in_facets even relies on this function not wrapping
1155 * any constraints that are already valid.
1156 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1157 * wraps->n_row is the number of actual wrapped constraints that have
1158 * been added.
1159 * If any of the wrapping problems results in a constraint that is
1160 * identical to "bound", then this means that "set" is unbounded in such
1161 * way that no wrapping is possible. If this happens then wraps->n_row
1162 * is reset to zero.
1163 * Similarly, if we want to bound the coefficients of the wrapping
1164 * constraints and a newly added wrapping constraint does not
1165 * satisfy the bound, then wraps->n_row is also reset to zero.
1167 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1168 isl_int *bound, __isl_keep isl_set *set)
1170 int l, m;
1171 int w;
1172 int added;
1173 isl_basic_map *bmap = info->bmap;
1174 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1176 w = wraps->mat->n_row;
1178 for (l = 0; l < bmap->n_ineq; ++l) {
1179 if (info->ineq[l] == STATUS_VALID ||
1180 info->ineq[l] == STATUS_REDUNDANT)
1181 continue;
1182 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1183 continue;
1184 if (isl_seq_eq(bound, bmap->ineq[l], len))
1185 continue;
1186 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1187 continue;
1189 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1190 if (added < 0)
1191 return -1;
1192 if (!added)
1193 goto unbounded;
1194 ++w;
1196 for (l = 0; l < bmap->n_eq; ++l) {
1197 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1198 continue;
1199 if (isl_seq_eq(bound, bmap->eq[l], len))
1200 continue;
1202 for (m = 0; m < 2; ++m) {
1203 if (info->eq[2 * l + m] == STATUS_VALID)
1204 continue;
1205 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1206 set, !m);
1207 if (added < 0)
1208 return -1;
1209 if (!added)
1210 goto unbounded;
1211 ++w;
1215 wraps->mat->n_row = w;
1216 return 0;
1217 unbounded:
1218 wraps->mat->n_row = 0;
1219 return 0;
1222 /* Check if the constraints in "wraps" from "first" until the last
1223 * are all valid for the basic set represented by "tab".
1224 * If not, wraps->n_row is set to zero.
1226 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1227 struct isl_tab *tab)
1229 int i;
1231 for (i = first; i < wraps->n_row; ++i) {
1232 enum isl_ineq_type type;
1233 type = isl_tab_ineq_type(tab, wraps->row[i]);
1234 if (type == isl_ineq_error)
1235 return -1;
1236 if (type == isl_ineq_redundant)
1237 continue;
1238 wraps->n_row = 0;
1239 return 0;
1242 return 0;
1245 /* Return a set that corresponds to the non-redundant constraints
1246 * (as recorded in tab) of bmap.
1248 * It's important to remove the redundant constraints as some
1249 * of the other constraints may have been modified after the
1250 * constraints were marked redundant.
1251 * In particular, a constraint may have been relaxed.
1252 * Redundant constraints are ignored when a constraint is relaxed
1253 * and should therefore continue to be ignored ever after.
1254 * Otherwise, the relaxation might be thwarted by some of
1255 * these constraints.
1257 * Update the underlying set to ensure that the dimension doesn't change.
1258 * Otherwise the integer divisions could get dropped if the tab
1259 * turns out to be empty.
1261 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1262 struct isl_tab *tab)
1264 isl_basic_set *bset;
1266 bmap = isl_basic_map_copy(bmap);
1267 bset = isl_basic_map_underlying_set(bmap);
1268 bset = isl_basic_set_cow(bset);
1269 bset = isl_basic_set_update_from_tab(bset, tab);
1270 return isl_set_from_basic_set(bset);
1273 /* Wrap the constraints of info->bmap that bound the facet defined
1274 * by inequality "k" around (the opposite of) this inequality to
1275 * include "set". "bound" may be used to store the negated inequality.
1276 * Since the wrapped constraints are not guaranteed to contain the whole
1277 * of info->bmap, we check them in check_wraps.
1278 * If any of the wrapped constraints turn out to be invalid, then
1279 * check_wraps will reset wrap->n_row to zero.
1281 static int add_wraps_around_facet(struct isl_wraps *wraps,
1282 struct isl_coalesce_info *info, int k, isl_int *bound,
1283 __isl_keep isl_set *set)
1285 struct isl_tab_undo *snap;
1286 int n;
1287 unsigned total = isl_basic_map_total_dim(info->bmap);
1289 snap = isl_tab_snap(info->tab);
1291 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1292 return -1;
1293 if (isl_tab_detect_redundant(info->tab) < 0)
1294 return -1;
1296 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1298 n = wraps->mat->n_row;
1299 if (add_wraps(wraps, info, bound, set) < 0)
1300 return -1;
1302 if (isl_tab_rollback(info->tab, snap) < 0)
1303 return -1;
1304 if (check_wraps(wraps->mat, n, info->tab) < 0)
1305 return -1;
1307 return 0;
1310 /* Given a basic set i with a constraint k that is adjacent to
1311 * basic set j, check if we can wrap
1312 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1313 * (always) around their ridges to include the other set.
1314 * If so, replace the pair of basic sets by their union.
1316 * All constraints of i (except k) are assumed to be valid or
1317 * cut constraints for j.
1318 * Wrapping the cut constraints to include basic map j may result
1319 * in constraints that are no longer valid of basic map i
1320 * we have to check that the resulting wrapping constraints are valid for i.
1321 * If "wrap_facet" is not set, then all constraints of i (except k)
1322 * are assumed to be valid for j.
1323 * ____ _____
1324 * / | / \
1325 * / || / |
1326 * \ || => \ |
1327 * \ || \ |
1328 * \___|| \____|
1331 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1332 struct isl_coalesce_info *info, int wrap_facet)
1334 enum isl_change change = isl_change_none;
1335 struct isl_wraps wraps;
1336 isl_ctx *ctx;
1337 isl_mat *mat;
1338 struct isl_set *set_i = NULL;
1339 struct isl_set *set_j = NULL;
1340 struct isl_vec *bound = NULL;
1341 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1343 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1344 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1345 ctx = isl_basic_map_get_ctx(info[i].bmap);
1346 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1347 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1348 1 + total);
1349 wraps_init(&wraps, mat, info, i, j);
1350 bound = isl_vec_alloc(ctx, 1 + total);
1351 if (!set_i || !set_j || !wraps.mat || !bound)
1352 goto error;
1354 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1355 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1357 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1358 wraps.mat->n_row = 1;
1360 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1361 goto error;
1362 if (!wraps.mat->n_row)
1363 goto unbounded;
1365 if (wrap_facet) {
1366 if (add_wraps_around_facet(&wraps, &info[i], k,
1367 bound->el, set_j) < 0)
1368 goto error;
1369 if (!wraps.mat->n_row)
1370 goto unbounded;
1373 change = fuse(i, j, info, wraps.mat, 0, 0);
1375 unbounded:
1376 wraps_free(&wraps);
1378 isl_set_free(set_i);
1379 isl_set_free(set_j);
1381 isl_vec_free(bound);
1383 return change;
1384 error:
1385 wraps_free(&wraps);
1386 isl_vec_free(bound);
1387 isl_set_free(set_i);
1388 isl_set_free(set_j);
1389 return isl_change_error;
1392 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1393 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1394 * add wrapping constraints to wrap.mat for all constraints
1395 * of basic map j that bound the part of basic map j that sticks out
1396 * of the cut constraint.
1397 * "set_i" is the underlying set of basic map i.
1398 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1400 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1401 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1402 * (with respect to the integer points), so we add t(x) >= 0 instead.
1403 * Otherwise, we wrap the constraints of basic map j that are not
1404 * redundant in this intersection and that are not already valid
1405 * for basic map i over basic map i.
1406 * Note that it is sufficient to wrap the constraints to include
1407 * basic map i, because we will only wrap the constraints that do
1408 * not include basic map i already. The wrapped constraint will
1409 * therefore be more relaxed compared to the original constraint.
1410 * Since the original constraint is valid for basic map j, so is
1411 * the wrapped constraint.
1413 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1414 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1415 struct isl_tab_undo *snap)
1417 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1418 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1419 return isl_stat_error;
1420 if (isl_tab_detect_redundant(info_j->tab) < 0)
1421 return isl_stat_error;
1423 if (info_j->tab->empty)
1424 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1425 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1426 return isl_stat_error;
1428 if (isl_tab_rollback(info_j->tab, snap) < 0)
1429 return isl_stat_error;
1431 return isl_stat_ok;
1434 /* Given a pair of basic maps i and j such that j sticks out
1435 * of i at n cut constraints, each time by at most one,
1436 * try to compute wrapping constraints and replace the two
1437 * basic maps by a single basic map.
1438 * The other constraints of i are assumed to be valid for j.
1439 * "set_i" is the underlying set of basic map i.
1440 * "wraps" has been initialized to be of the right size.
1442 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1443 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1444 * of basic map j that bound the part of basic map j that sticks out
1445 * of the cut constraint.
1447 * If any wrapping fails, i.e., if we cannot wrap to touch
1448 * the union, then we give up.
1449 * Otherwise, the pair of basic maps is replaced by their union.
1451 static enum isl_change try_wrap_in_facets(int i, int j,
1452 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1453 __isl_keep isl_set *set_i)
1455 int k, l, w;
1456 unsigned total;
1457 struct isl_tab_undo *snap;
1459 total = isl_basic_map_total_dim(info[i].bmap);
1461 snap = isl_tab_snap(info[j].tab);
1463 wraps->mat->n_row = 0;
1465 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1466 for (l = 0; l < 2; ++l) {
1467 if (info[i].eq[2 * k + l] != STATUS_CUT)
1468 continue;
1469 w = wraps->mat->n_row++;
1470 if (l == 0)
1471 isl_seq_neg(wraps->mat->row[w],
1472 info[i].bmap->eq[k], 1 + total);
1473 else
1474 isl_seq_cpy(wraps->mat->row[w],
1475 info[i].bmap->eq[k], 1 + total);
1476 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1477 return isl_change_error;
1479 if (!wraps->mat->n_row)
1480 return isl_change_none;
1484 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1485 if (info[i].ineq[k] != STATUS_CUT)
1486 continue;
1487 w = wraps->mat->n_row++;
1488 isl_seq_cpy(wraps->mat->row[w],
1489 info[i].bmap->ineq[k], 1 + total);
1490 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1491 return isl_change_error;
1493 if (!wraps->mat->n_row)
1494 return isl_change_none;
1497 return fuse(i, j, info, wraps->mat, 0, 1);
1500 /* Given a pair of basic maps i and j such that j sticks out
1501 * of i at n cut constraints, each time by at most one,
1502 * try to compute wrapping constraints and replace the two
1503 * basic maps by a single basic map.
1504 * The other constraints of i are assumed to be valid for j.
1506 * The core computation is performed by try_wrap_in_facets.
1507 * This function simply extracts an underlying set representation
1508 * of basic map i and initializes the data structure for keeping
1509 * track of wrapping constraints.
1511 static enum isl_change wrap_in_facets(int i, int j, int n,
1512 struct isl_coalesce_info *info)
1514 enum isl_change change = isl_change_none;
1515 struct isl_wraps wraps;
1516 isl_ctx *ctx;
1517 isl_mat *mat;
1518 isl_set *set_i = NULL;
1519 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1520 int max_wrap;
1522 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1523 return isl_change_error;
1525 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1526 max_wrap *= n;
1528 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1529 ctx = isl_basic_map_get_ctx(info[i].bmap);
1530 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1531 wraps_init(&wraps, mat, info, i, j);
1532 if (!set_i || !wraps.mat)
1533 goto error;
1535 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1537 wraps_free(&wraps);
1538 isl_set_free(set_i);
1540 return change;
1541 error:
1542 wraps_free(&wraps);
1543 isl_set_free(set_i);
1544 return isl_change_error;
1547 /* Return the effect of inequality "ineq" on the tableau "tab",
1548 * after relaxing the constant term of "ineq" by one.
1550 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1552 enum isl_ineq_type type;
1554 isl_int_add_ui(ineq[0], ineq[0], 1);
1555 type = isl_tab_ineq_type(tab, ineq);
1556 isl_int_sub_ui(ineq[0], ineq[0], 1);
1558 return type;
1561 /* Given two basic sets i and j,
1562 * check if relaxing all the cut constraints of i by one turns
1563 * them into valid constraint for j and check if we can wrap in
1564 * the bits that are sticking out.
1565 * If so, replace the pair by their union.
1567 * We first check if all relaxed cut inequalities of i are valid for j
1568 * and then try to wrap in the intersections of the relaxed cut inequalities
1569 * with j.
1571 * During this wrapping, we consider the points of j that lie at a distance
1572 * of exactly 1 from i. In particular, we ignore the points that lie in
1573 * between this lower-dimensional space and the basic map i.
1574 * We can therefore only apply this to integer maps.
1575 * ____ _____
1576 * / ___|_ / \
1577 * / | | / |
1578 * \ | | => \ |
1579 * \|____| \ |
1580 * \___| \____/
1582 * _____ ______
1583 * | ____|_ | \
1584 * | | | | |
1585 * | | | => | |
1586 * |_| | | |
1587 * |_____| \______|
1589 * _______
1590 * | |
1591 * | |\ |
1592 * | | \ |
1593 * | | \ |
1594 * | | \|
1595 * | | \
1596 * | |_____\
1597 * | |
1598 * |_______|
1600 * Wrapping can fail if the result of wrapping one of the facets
1601 * around its edges does not produce any new facet constraint.
1602 * In particular, this happens when we try to wrap in unbounded sets.
1604 * _______________________________________________________________________
1606 * | ___
1607 * | | |
1608 * |_| |_________________________________________________________________
1609 * |___|
1611 * The following is not an acceptable result of coalescing the above two
1612 * sets as it includes extra integer points.
1613 * _______________________________________________________________________
1615 * |
1616 * |
1618 * \______________________________________________________________________
1620 static enum isl_change can_wrap_in_set(int i, int j,
1621 struct isl_coalesce_info *info)
1623 int k, l;
1624 int n;
1625 unsigned total;
1627 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1628 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1629 return isl_change_none;
1631 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1632 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1633 if (n == 0)
1634 return isl_change_none;
1636 total = isl_basic_map_total_dim(info[i].bmap);
1637 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1638 for (l = 0; l < 2; ++l) {
1639 enum isl_ineq_type type;
1641 if (info[i].eq[2 * k + l] != STATUS_CUT)
1642 continue;
1644 if (l == 0)
1645 isl_seq_neg(info[i].bmap->eq[k],
1646 info[i].bmap->eq[k], 1 + total);
1647 type = type_of_relaxed(info[j].tab,
1648 info[i].bmap->eq[k]);
1649 if (l == 0)
1650 isl_seq_neg(info[i].bmap->eq[k],
1651 info[i].bmap->eq[k], 1 + total);
1652 if (type == isl_ineq_error)
1653 return isl_change_error;
1654 if (type != isl_ineq_redundant)
1655 return isl_change_none;
1659 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1660 enum isl_ineq_type type;
1662 if (info[i].ineq[k] != STATUS_CUT)
1663 continue;
1665 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1666 if (type == isl_ineq_error)
1667 return isl_change_error;
1668 if (type != isl_ineq_redundant)
1669 return isl_change_none;
1672 return wrap_in_facets(i, j, n, info);
1675 /* Check if either i or j has only cut constraints that can
1676 * be used to wrap in (a facet of) the other basic set.
1677 * if so, replace the pair by their union.
1679 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1681 enum isl_change change = isl_change_none;
1683 change = can_wrap_in_set(i, j, info);
1684 if (change != isl_change_none)
1685 return change;
1687 change = can_wrap_in_set(j, i, info);
1688 return change;
1691 /* At least one of the basic maps has an equality that is adjacent
1692 * to inequality. Make sure that only one of the basic maps has
1693 * such an equality and that the other basic map has exactly one
1694 * inequality adjacent to an equality.
1695 * If the other basic map does not have such an inequality, then
1696 * check if all its constraints are either valid or cut constraints
1697 * and, if so, try wrapping in the first map into the second.
1699 * We call the basic map that has the inequality "i" and the basic
1700 * map that has the equality "j".
1701 * If "i" has any "cut" (in)equality, then relaxing the inequality
1702 * by one would not result in a basic map that contains the other
1703 * basic map. However, it may still be possible to wrap in the other
1704 * basic map.
1706 static enum isl_change check_adj_eq(int i, int j,
1707 struct isl_coalesce_info *info)
1709 enum isl_change change = isl_change_none;
1710 int k;
1711 int any_cut;
1713 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1714 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1715 /* ADJ EQ TOO MANY */
1716 return isl_change_none;
1718 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1719 return check_adj_eq(j, i, info);
1721 /* j has an equality adjacent to an inequality in i */
1723 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1724 if (all_valid_or_cut(&info[i]))
1725 return can_wrap_in_set(i, j, info);
1726 return isl_change_none;
1728 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1729 return isl_change_none;
1730 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1731 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1732 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1733 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1734 /* ADJ EQ TOO MANY */
1735 return isl_change_none;
1737 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1738 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1739 break;
1741 if (!any_cut) {
1742 change = is_adj_eq_extension(i, j, k, info);
1743 if (change != isl_change_none)
1744 return change;
1747 change = can_wrap_in_facet(i, j, k, info, any_cut);
1749 return change;
1752 /* The two basic maps lie on adjacent hyperplanes. In particular,
1753 * basic map "i" has an equality that lies parallel to basic map "j".
1754 * Check if we can wrap the facets around the parallel hyperplanes
1755 * to include the other set.
1757 * We perform basically the same operations as can_wrap_in_facet,
1758 * except that we don't need to select a facet of one of the sets.
1760 * \\ \\
1761 * \\ => \\
1762 * \ \|
1764 * If there is more than one equality of "i" adjacent to an equality of "j",
1765 * then the result will satisfy one or more equalities that are a linear
1766 * combination of these equalities. These will be encoded as pairs
1767 * of inequalities in the wrapping constraints and need to be made
1768 * explicit.
1770 static enum isl_change check_eq_adj_eq(int i, int j,
1771 struct isl_coalesce_info *info)
1773 int k;
1774 enum isl_change change = isl_change_none;
1775 int detect_equalities = 0;
1776 struct isl_wraps wraps;
1777 isl_ctx *ctx;
1778 isl_mat *mat;
1779 struct isl_set *set_i = NULL;
1780 struct isl_set *set_j = NULL;
1781 struct isl_vec *bound = NULL;
1782 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1784 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1785 detect_equalities = 1;
1787 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1788 if (info[i].eq[k] == STATUS_ADJ_EQ)
1789 break;
1791 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1792 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1793 ctx = isl_basic_map_get_ctx(info[i].bmap);
1794 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1795 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1796 1 + total);
1797 wraps_init(&wraps, mat, info, i, j);
1798 bound = isl_vec_alloc(ctx, 1 + total);
1799 if (!set_i || !set_j || !wraps.mat || !bound)
1800 goto error;
1802 if (k % 2 == 0)
1803 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1804 else
1805 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1806 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1808 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1809 wraps.mat->n_row = 1;
1811 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1812 goto error;
1813 if (!wraps.mat->n_row)
1814 goto unbounded;
1816 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1817 isl_seq_neg(bound->el, bound->el, 1 + total);
1819 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1820 wraps.mat->n_row++;
1822 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1823 goto error;
1824 if (!wraps.mat->n_row)
1825 goto unbounded;
1827 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1829 if (0) {
1830 error: change = isl_change_error;
1832 unbounded:
1834 wraps_free(&wraps);
1835 isl_set_free(set_i);
1836 isl_set_free(set_j);
1837 isl_vec_free(bound);
1839 return change;
1842 /* Initialize the "eq" and "ineq" fields of "info".
1844 static void init_status(struct isl_coalesce_info *info)
1846 info->eq = info->ineq = NULL;
1849 /* Set info->eq to the positions of the equalities of info->bmap
1850 * with respect to the basic map represented by "tab".
1851 * If info->eq has already been computed, then do not compute it again.
1853 static void set_eq_status_in(struct isl_coalesce_info *info,
1854 struct isl_tab *tab)
1856 if (info->eq)
1857 return;
1858 info->eq = eq_status_in(info->bmap, tab);
1861 /* Set info->ineq to the positions of the inequalities of info->bmap
1862 * with respect to the basic map represented by "tab".
1863 * If info->ineq has already been computed, then do not compute it again.
1865 static void set_ineq_status_in(struct isl_coalesce_info *info,
1866 struct isl_tab *tab)
1868 if (info->ineq)
1869 return;
1870 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1873 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1874 * This function assumes that init_status has been called on "info" first,
1875 * after which the "eq" and "ineq" fields may or may not have been
1876 * assigned a newly allocated array.
1878 static void clear_status(struct isl_coalesce_info *info)
1880 free(info->eq);
1881 free(info->ineq);
1884 /* Check if the union of the given pair of basic maps
1885 * can be represented by a single basic map.
1886 * If so, replace the pair by the single basic map and return
1887 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1888 * Otherwise, return isl_change_none.
1889 * The two basic maps are assumed to live in the same local space.
1890 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1891 * to have been initialized by the caller, either to NULL or
1892 * to valid information.
1894 * We first check the effect of each constraint of one basic map
1895 * on the other basic map.
1896 * The constraint may be
1897 * redundant the constraint is redundant in its own
1898 * basic map and should be ignore and removed
1899 * in the end
1900 * valid all (integer) points of the other basic map
1901 * satisfy the constraint
1902 * separate no (integer) point of the other basic map
1903 * satisfies the constraint
1904 * cut some but not all points of the other basic map
1905 * satisfy the constraint
1906 * adj_eq the given constraint is adjacent (on the outside)
1907 * to an equality of the other basic map
1908 * adj_ineq the given constraint is adjacent (on the outside)
1909 * to an inequality of the other basic map
1911 * We consider seven cases in which we can replace the pair by a single
1912 * basic map. We ignore all "redundant" constraints.
1914 * 1. all constraints of one basic map are valid
1915 * => the other basic map is a subset and can be removed
1917 * 2. all constraints of both basic maps are either "valid" or "cut"
1918 * and the facets corresponding to the "cut" constraints
1919 * of one of the basic maps lies entirely inside the other basic map
1920 * => the pair can be replaced by a basic map consisting
1921 * of the valid constraints in both basic maps
1923 * 3. there is a single pair of adjacent inequalities
1924 * (all other constraints are "valid")
1925 * => the pair can be replaced by a basic map consisting
1926 * of the valid constraints in both basic maps
1928 * 4. one basic map has a single adjacent inequality, while the other
1929 * constraints are "valid". The other basic map has some
1930 * "cut" constraints, but replacing the adjacent inequality by
1931 * its opposite and adding the valid constraints of the other
1932 * basic map results in a subset of the other basic map
1933 * => the pair can be replaced by a basic map consisting
1934 * of the valid constraints in both basic maps
1936 * 5. there is a single adjacent pair of an inequality and an equality,
1937 * the other constraints of the basic map containing the inequality are
1938 * "valid". Moreover, if the inequality the basic map is relaxed
1939 * and then turned into an equality, then resulting facet lies
1940 * entirely inside the other basic map
1941 * => the pair can be replaced by the basic map containing
1942 * the inequality, with the inequality relaxed.
1944 * 6. there is a single adjacent pair of an inequality and an equality,
1945 * the other constraints of the basic map containing the inequality are
1946 * "valid". Moreover, the facets corresponding to both
1947 * the inequality and the equality can be wrapped around their
1948 * ridges to include the other basic map
1949 * => the pair can be replaced by a basic map consisting
1950 * of the valid constraints in both basic maps together
1951 * with all wrapping constraints
1953 * 7. one of the basic maps extends beyond the other by at most one.
1954 * Moreover, the facets corresponding to the cut constraints and
1955 * the pieces of the other basic map at offset one from these cut
1956 * constraints can be wrapped around their ridges to include
1957 * the union of the two basic maps
1958 * => the pair can be replaced by a basic map consisting
1959 * of the valid constraints in both basic maps together
1960 * with all wrapping constraints
1962 * 8. the two basic maps live in adjacent hyperplanes. In principle
1963 * such sets can always be combined through wrapping, but we impose
1964 * that there is only one such pair, to avoid overeager coalescing.
1966 * Throughout the computation, we maintain a collection of tableaus
1967 * corresponding to the basic maps. When the basic maps are dropped
1968 * or combined, the tableaus are modified accordingly.
1970 static enum isl_change coalesce_local_pair_reuse(int i, int j,
1971 struct isl_coalesce_info *info)
1973 enum isl_change change = isl_change_none;
1975 set_eq_status_in(&info[i], info[j].tab);
1976 if (info[i].bmap->n_eq && !info[i].eq)
1977 goto error;
1978 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1979 goto error;
1980 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1981 goto done;
1983 set_eq_status_in(&info[j], info[i].tab);
1984 if (info[j].bmap->n_eq && !info[j].eq)
1985 goto error;
1986 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1987 goto error;
1988 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1989 goto done;
1991 set_ineq_status_in(&info[i], info[j].tab);
1992 if (info[i].bmap->n_ineq && !info[i].ineq)
1993 goto error;
1994 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1995 goto error;
1996 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1997 goto done;
1999 set_ineq_status_in(&info[j], info[i].tab);
2000 if (info[j].bmap->n_ineq && !info[j].ineq)
2001 goto error;
2002 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2003 goto error;
2004 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2005 goto done;
2007 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2008 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2009 drop(&info[j]);
2010 change = isl_change_drop_second;
2011 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2012 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2013 drop(&info[i]);
2014 change = isl_change_drop_first;
2015 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2016 change = check_eq_adj_eq(i, j, info);
2017 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2018 change = check_eq_adj_eq(j, i, info);
2019 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2020 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2021 change = check_adj_eq(i, j, info);
2022 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2023 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2024 /* Can't happen */
2025 /* BAD ADJ INEQ */
2026 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2027 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2028 change = check_adj_ineq(i, j, info);
2029 } else {
2030 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2031 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2032 change = check_facets(i, j, info);
2033 if (change == isl_change_none)
2034 change = check_wrap(i, j, info);
2037 done:
2038 clear_status(&info[i]);
2039 clear_status(&info[j]);
2040 return change;
2041 error:
2042 clear_status(&info[i]);
2043 clear_status(&info[j]);
2044 return isl_change_error;
2047 /* Check if the union of the given pair of basic maps
2048 * can be represented by a single basic map.
2049 * If so, replace the pair by the single basic map and return
2050 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2051 * Otherwise, return isl_change_none.
2052 * The two basic maps are assumed to live in the same local space.
2054 static enum isl_change coalesce_local_pair(int i, int j,
2055 struct isl_coalesce_info *info)
2057 init_status(&info[i]);
2058 init_status(&info[j]);
2059 return coalesce_local_pair_reuse(i, j, info);
2062 /* Shift the integer division at position "div" of the basic map
2063 * represented by "info" by "shift".
2065 * That is, if the integer division has the form
2067 * floor(f(x)/d)
2069 * then replace it by
2071 * floor((f(x) + shift * d)/d) - shift
2073 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
2075 unsigned total;
2077 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2078 if (!info->bmap)
2079 return -1;
2081 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2082 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2083 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2084 return -1;
2086 return 0;
2089 /* Check if some of the divs in the basic map represented by "info1"
2090 * are shifts of the corresponding divs in the basic map represented
2091 * by "info2". If so, align them with those of "info2".
2092 * Only do this if "info1" and "info2" have the same number
2093 * of integer divisions.
2095 * An integer division is considered to be a shift of another integer
2096 * division if one is equal to the other plus a constant.
2098 * In particular, for each pair of integer divisions, if both are known,
2099 * have identical coefficients (apart from the constant term) and
2100 * if the difference between the constant terms (taking into account
2101 * the denominator) is an integer, then move the difference outside.
2102 * That is, if one integer division is of the form
2104 * floor((f(x) + c_1)/d)
2106 * while the other is of the form
2108 * floor((f(x) + c_2)/d)
2110 * and n = (c_2 - c_1)/d is an integer, then replace the first
2111 * integer division by
2113 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2115 static int harmonize_divs(struct isl_coalesce_info *info1,
2116 struct isl_coalesce_info *info2)
2118 int i;
2119 int total;
2121 if (!info1->bmap || !info2->bmap)
2122 return -1;
2124 if (info1->bmap->n_div != info2->bmap->n_div)
2125 return 0;
2126 if (info1->bmap->n_div == 0)
2127 return 0;
2129 total = isl_basic_map_total_dim(info1->bmap);
2130 for (i = 0; i < info1->bmap->n_div; ++i) {
2131 isl_int d;
2132 int r = 0;
2134 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
2135 isl_int_is_zero(info2->bmap->div[i][0]))
2136 continue;
2137 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2138 continue;
2139 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
2140 continue;
2141 if (!isl_seq_eq(info1->bmap->div[i] + 2,
2142 info2->bmap->div[i] + 2, total))
2143 continue;
2144 isl_int_init(d);
2145 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
2146 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
2147 isl_int_divexact(d, d, info1->bmap->div[i][0]);
2148 r = shift_div(info1, i, d);
2150 isl_int_clear(d);
2151 if (r < 0)
2152 return -1;
2155 return 0;
2158 /* Do the two basic maps live in the same local space, i.e.,
2159 * do they have the same (known) divs?
2160 * If either basic map has any unknown divs, then we can only assume
2161 * that they do not live in the same local space.
2163 static int same_divs(__isl_keep isl_basic_map *bmap1,
2164 __isl_keep isl_basic_map *bmap2)
2166 int i;
2167 int known;
2168 int total;
2170 if (!bmap1 || !bmap2)
2171 return -1;
2172 if (bmap1->n_div != bmap2->n_div)
2173 return 0;
2175 if (bmap1->n_div == 0)
2176 return 1;
2178 known = isl_basic_map_divs_known(bmap1);
2179 if (known < 0 || !known)
2180 return known;
2181 known = isl_basic_map_divs_known(bmap2);
2182 if (known < 0 || !known)
2183 return known;
2185 total = isl_basic_map_total_dim(bmap1);
2186 for (i = 0; i < bmap1->n_div; ++i)
2187 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2188 return 0;
2190 return 1;
2193 /* Expand info->tab in the same way info->bmap was expanded in
2194 * isl_basic_map_expand_divs using the expansion "exp" and
2195 * update info->ineq with respect to the redundant constraints
2196 * in the resulting tableau. "bmap" is the original version
2197 * of info->bmap, i.e., the one that corresponds to the current
2198 * state of info->tab. The number of constraints in "bmap"
2199 * is assumed to be the same as the number of constraints
2200 * in info->tab. This is required to be able to detect
2201 * the extra constraints in info->bmap.
2203 * In particular, introduce extra variables corresponding
2204 * to the extra integer divisions and add the div constraints
2205 * that were added to info->bmap after info->tab was created
2206 * from the original info->bmap.
2207 * info->ineq was computed without a tableau and therefore
2208 * does not take into account the redundant constraints
2209 * in the tableau. Mark them here.
2211 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2212 __isl_keep isl_basic_map *bmap)
2214 unsigned total, pos, n_div;
2215 int extra_var;
2216 int i, n, j, n_ineq;
2217 unsigned n_eq;
2219 if (!bmap)
2220 return isl_stat_error;
2221 if (bmap->n_eq + bmap->n_ineq != info->tab->n_con)
2222 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2223 "original tableau does not correspond "
2224 "to original basic map", return isl_stat_error);
2226 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2227 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2228 pos = total - n_div;
2229 extra_var = total - info->tab->n_var;
2230 n = n_div - extra_var;
2232 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2233 return isl_stat_error;
2234 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2235 return isl_stat_error;
2237 i = 0;
2238 for (j = 0; j < n_div; ++j) {
2239 if (i < n && exp[i] == j) {
2240 ++i;
2241 continue;
2243 if (isl_tab_insert_var(info->tab, pos + j) < 0)
2244 return isl_stat_error;
2247 n_ineq = info->tab->n_con - info->tab->n_eq;
2248 for (i = n_ineq; i < info->bmap->n_ineq; ++i)
2249 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2250 return isl_stat_error;
2252 n_eq = info->bmap->n_eq;
2253 for (i = 0; i < info->bmap->n_ineq; ++i) {
2254 if (isl_tab_is_redundant(info->tab, n_eq + i))
2255 info->ineq[i] = STATUS_REDUNDANT;
2258 return isl_stat_ok;
2261 /* Check if the union of the basic maps represented by info[i] and info[j]
2262 * can be represented by a single basic map,
2263 * after expanding the divs of info[i] to match those of info[j].
2264 * If so, replace the pair by the single basic map and return
2265 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2266 * Otherwise, return isl_change_none.
2268 * The caller has already checked for info[j] being a subset of info[i].
2269 * If some of the divs of info[j] are unknown, then the expanded info[i]
2270 * will not have the corresponding div constraints. The other patterns
2271 * therefore cannot apply. Skip the computation in this case.
2273 * The expansion is performed using the divs "div" and expansion "exp"
2274 * computed by the caller.
2275 * info[i].bmap has already been expanded and the result is passed in
2276 * as "bmap".
2277 * The "eq" and "ineq" fields of info[i] reflect the status of
2278 * the constraints of the expanded "bmap" with respect to info[j].tab.
2279 * However, inequality constraints that are redundant in info[i].tab
2280 * have not yet been marked as such because no tableau was available.
2282 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2283 * updating info[i].ineq with respect to the redundant constraints.
2284 * Then try and coalesce the expanded info[i] with info[j],
2285 * reusing the information in info[i].eq and info[i].ineq.
2286 * If this does not result in any coalescing or if it results in info[j]
2287 * getting dropped (which should not happen in practice, since the case
2288 * of info[j] being a subset of info[i] has already been checked by
2289 * the caller), then revert info[i] to its original state.
2291 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2292 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2293 int *exp)
2295 isl_bool known;
2296 isl_basic_map *bmap_i;
2297 struct isl_tab_undo *snap;
2298 enum isl_change change = isl_change_none;
2300 known = isl_basic_map_divs_known(info[j].bmap);
2301 if (known < 0 || !known) {
2302 clear_status(&info[i]);
2303 isl_basic_map_free(bmap);
2304 return known < 0 ? isl_change_error : isl_change_none;
2307 bmap_i = info[i].bmap;
2308 info[i].bmap = isl_basic_map_copy(bmap);
2309 snap = isl_tab_snap(info[i].tab);
2310 if (!info[i].bmap || expand_tab(&info[i], exp, bmap_i) < 0)
2311 change = isl_change_error;
2313 init_status(&info[j]);
2314 if (change == isl_change_none)
2315 change = coalesce_local_pair_reuse(i, j, info);
2316 else
2317 clear_status(&info[i]);
2318 if (change != isl_change_none && change != isl_change_drop_second) {
2319 isl_basic_map_free(bmap_i);
2320 } else {
2321 isl_basic_map_free(info[i].bmap);
2322 info[i].bmap = bmap_i;
2324 if (isl_tab_rollback(info[i].tab, snap) < 0)
2325 change = isl_change_error;
2328 isl_basic_map_free(bmap);
2329 return change;
2332 /* Check if the union of "bmap" and the basic map represented by info[j]
2333 * can be represented by a single basic map,
2334 * after expanding the divs of "bmap" to match those of info[j].
2335 * If so, replace the pair by the single basic map and return
2336 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2337 * Otherwise, return isl_change_none.
2339 * In particular, check if the expanded "bmap" contains the basic map
2340 * represented by the tableau info[j].tab.
2341 * The expansion is performed using the divs "div" and expansion "exp"
2342 * computed by the caller.
2343 * Then we check if all constraints of the expanded "bmap" are valid for
2344 * info[j].tab.
2346 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2347 * In this case, the positions of the constraints of info[i].bmap
2348 * with respect to the basic map represented by info[j] are stored
2349 * in info[i].
2351 * If the expanded "bmap" does not contain the basic map
2352 * represented by the tableau info[j].tab and if "i" is not -1,
2353 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2354 * as well and check if that results in coalescing.
2356 static enum isl_change coalesce_with_expanded_divs(
2357 __isl_keep isl_basic_map *bmap, int i, int j,
2358 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2360 enum isl_change change = isl_change_none;
2361 struct isl_coalesce_info info_local, *info_i;
2363 info_i = i >= 0 ? &info[i] : &info_local;
2364 init_status(info_i);
2365 bmap = isl_basic_map_copy(bmap);
2366 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2368 if (!bmap)
2369 goto error;
2371 info_i->eq = eq_status_in(bmap, info[j].tab);
2372 if (bmap->n_eq && !info_i->eq)
2373 goto error;
2374 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2375 goto error;
2376 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2377 goto done;
2379 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2380 if (bmap->n_ineq && !info_i->ineq)
2381 goto error;
2382 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2383 goto error;
2384 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2385 goto done;
2387 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2388 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2389 drop(&info[j]);
2390 change = isl_change_drop_second;
2393 if (change == isl_change_none && i != -1)
2394 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2396 done:
2397 isl_basic_map_free(bmap);
2398 clear_status(info_i);
2399 return change;
2400 error:
2401 isl_basic_map_free(bmap);
2402 clear_status(info_i);
2403 return isl_change_error;
2406 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2407 * can be represented by a single basic map,
2408 * after aligning the divs of "bmap_i" to match those of info[j].
2409 * If so, replace the pair by the single basic map and return
2410 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2411 * Otherwise, return isl_change_none.
2413 * In particular, check if "bmap_i" contains the basic map represented by
2414 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2415 * Note that this can only succeed if the number of divs of "bmap_i"
2416 * is smaller than (or equal to) the number of divs of info[j].
2418 * We first check if the divs of "bmap_i" are all known and form a subset
2419 * of those of info[j].bmap. If so, we pass control over to
2420 * coalesce_with_expanded_divs.
2422 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2424 static enum isl_change coalesce_after_aligning_divs(
2425 __isl_keep isl_basic_map *bmap_i, int i, int j,
2426 struct isl_coalesce_info *info)
2428 int known;
2429 isl_mat *div_i, *div_j, *div;
2430 int *exp1 = NULL;
2431 int *exp2 = NULL;
2432 isl_ctx *ctx;
2433 enum isl_change change;
2435 known = isl_basic_map_divs_known(bmap_i);
2436 if (known < 0 || !known)
2437 return known;
2439 ctx = isl_basic_map_get_ctx(bmap_i);
2441 div_i = isl_basic_map_get_divs(bmap_i);
2442 div_j = isl_basic_map_get_divs(info[j].bmap);
2444 if (!div_i || !div_j)
2445 goto error;
2447 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2448 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2449 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2450 goto error;
2452 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2453 if (!div)
2454 goto error;
2456 if (div->n_row == div_j->n_row)
2457 change = coalesce_with_expanded_divs(bmap_i,
2458 i, j, info, div, exp1);
2459 else
2460 change = isl_change_none;
2462 isl_mat_free(div);
2464 isl_mat_free(div_i);
2465 isl_mat_free(div_j);
2467 free(exp2);
2468 free(exp1);
2470 return change;
2471 error:
2472 isl_mat_free(div_i);
2473 isl_mat_free(div_j);
2474 free(exp1);
2475 free(exp2);
2476 return isl_change_error;
2479 /* Check if basic map "j" is a subset of basic map "i" after
2480 * exploiting the extra equalities of "j" to simplify the divs of "i".
2481 * If so, remove basic map "j" and return isl_change_drop_second.
2483 * If "j" does not have any equalities or if they are the same
2484 * as those of "i", then we cannot exploit them to simplify the divs.
2485 * Similarly, if there are no divs in "i", then they cannot be simplified.
2486 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2487 * then "j" cannot be a subset of "i".
2489 * Otherwise, we intersect "i" with the affine hull of "j" and then
2490 * check if "j" is a subset of the result after aligning the divs.
2491 * If so, then "j" is definitely a subset of "i" and can be removed.
2492 * Note that if after intersection with the affine hull of "j".
2493 * "i" still has more divs than "j", then there is no way we can
2494 * align the divs of "i" to those of "j".
2496 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2497 struct isl_coalesce_info *info)
2499 isl_basic_map *hull_i, *hull_j, *bmap_i;
2500 int equal, empty;
2501 enum isl_change change;
2503 if (info[j].bmap->n_eq == 0)
2504 return isl_change_none;
2505 if (info[i].bmap->n_div == 0)
2506 return isl_change_none;
2508 hull_i = isl_basic_map_copy(info[i].bmap);
2509 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2510 hull_j = isl_basic_map_copy(info[j].bmap);
2511 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2513 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2514 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2515 empty = isl_basic_map_plain_is_empty(hull_j);
2516 isl_basic_map_free(hull_i);
2518 if (equal < 0 || equal || empty < 0 || empty) {
2519 isl_basic_map_free(hull_j);
2520 if (equal < 0 || empty < 0)
2521 return isl_change_error;
2522 return isl_change_none;
2525 bmap_i = isl_basic_map_copy(info[i].bmap);
2526 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2527 if (!bmap_i)
2528 return isl_change_error;
2530 if (bmap_i->n_div > info[j].bmap->n_div) {
2531 isl_basic_map_free(bmap_i);
2532 return isl_change_none;
2535 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2537 isl_basic_map_free(bmap_i);
2539 return change;
2542 /* Check if the union of and the basic maps represented by info[i] and info[j]
2543 * can be represented by a single basic map, by aligning or equating
2544 * their integer divisions.
2545 * If so, replace the pair by the single basic map and return
2546 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2547 * Otherwise, return isl_change_none.
2549 * Note that we only perform any test if the number of divs is different
2550 * in the two basic maps. In case the number of divs is the same,
2551 * we have already established that the divs are different
2552 * in the two basic maps.
2553 * In particular, if the number of divs of basic map i is smaller than
2554 * the number of divs of basic map j, then we check if j is a subset of i
2555 * and vice versa.
2557 static enum isl_change coalesce_divs(int i, int j,
2558 struct isl_coalesce_info *info)
2560 enum isl_change change = isl_change_none;
2562 if (info[i].bmap->n_div < info[j].bmap->n_div)
2563 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2564 if (change != isl_change_none)
2565 return change;
2567 if (info[j].bmap->n_div < info[i].bmap->n_div)
2568 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2569 if (change != isl_change_none)
2570 return invert_change(change);
2572 change = coalesce_subset_with_equalities(i, j, info);
2573 if (change != isl_change_none)
2574 return change;
2576 change = coalesce_subset_with_equalities(j, i, info);
2577 if (change != isl_change_none)
2578 return invert_change(change);
2580 return isl_change_none;
2583 /* Does "bmap" involve any divs that themselves refer to divs?
2585 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2587 int i;
2588 unsigned total;
2589 unsigned n_div;
2591 total = isl_basic_map_dim(bmap, isl_dim_all);
2592 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2593 total -= n_div;
2595 for (i = 0; i < n_div; ++i)
2596 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2597 n_div) != -1)
2598 return 1;
2600 return 0;
2603 /* Return a list of affine expressions, one for each integer division
2604 * in "bmap_i". For each integer division that also appears in "bmap_j",
2605 * the affine expression is set to NaN. The number of NaNs in the list
2606 * is equal to the number of integer divisions in "bmap_j".
2607 * For the other integer divisions of "bmap_i", the corresponding
2608 * element in the list is a purely affine expression equal to the integer
2609 * division in "hull".
2610 * If no such list can be constructed, then the number of elements
2611 * in the returned list is smaller than the number of integer divisions
2612 * in "bmap_i".
2614 static __isl_give isl_aff_list *set_up_substitutions(
2615 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2616 __isl_take isl_basic_map *hull)
2618 unsigned n_div_i, n_div_j, total;
2619 isl_ctx *ctx;
2620 isl_local_space *ls;
2621 isl_basic_set *wrap_hull;
2622 isl_aff *aff_nan;
2623 isl_aff_list *list;
2624 int i, j;
2626 if (!hull)
2627 return NULL;
2629 ctx = isl_basic_map_get_ctx(hull);
2631 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2632 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2633 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2635 ls = isl_basic_map_get_local_space(bmap_i);
2636 ls = isl_local_space_wrap(ls);
2637 wrap_hull = isl_basic_map_wrap(hull);
2639 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2640 list = isl_aff_list_alloc(ctx, n_div_i);
2642 j = 0;
2643 for (i = 0; i < n_div_i; ++i) {
2644 isl_aff *aff;
2646 if (j < n_div_j &&
2647 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2648 ++j;
2649 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2650 continue;
2652 if (n_div_i - i <= n_div_j - j)
2653 break;
2655 aff = isl_local_space_get_div(ls, i);
2656 aff = isl_aff_substitute_equalities(aff,
2657 isl_basic_set_copy(wrap_hull));
2658 aff = isl_aff_floor(aff);
2659 if (!aff)
2660 goto error;
2661 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2662 isl_aff_free(aff);
2663 break;
2666 list = isl_aff_list_add(list, aff);
2669 isl_aff_free(aff_nan);
2670 isl_local_space_free(ls);
2671 isl_basic_set_free(wrap_hull);
2673 return list;
2674 error:
2675 isl_aff_free(aff_nan);
2676 isl_local_space_free(ls);
2677 isl_basic_set_free(wrap_hull);
2678 isl_aff_list_free(list);
2679 return NULL;
2682 /* Add variables to info->bmap and info->tab corresponding to the elements
2683 * in "list" that are not set to NaN.
2684 * "extra_var" is the number of these elements.
2685 * "dim" is the offset in the variables of "tab" where we should
2686 * start considering the elements in "list".
2687 * When this function returns, the total number of variables in "tab"
2688 * is equal to "dim" plus the number of elements in "list".
2690 * The newly added existentially quantified variables are not given
2691 * an explicit representation because the corresponding div constraints
2692 * do not appear in info->bmap. These constraints are not added
2693 * to info->bmap because for internal consistency, they would need to
2694 * be added to info->tab as well, where they could combine with the equality
2695 * that is added later to result in constraints that do not hold
2696 * in the original input.
2698 static int add_sub_vars(struct isl_coalesce_info *info,
2699 __isl_keep isl_aff_list *list, int dim, int extra_var)
2701 int i, j, n, d;
2702 isl_space *space;
2704 space = isl_basic_map_get_space(info->bmap);
2705 info->bmap = isl_basic_map_cow(info->bmap);
2706 info->bmap = isl_basic_map_extend_space(info->bmap, space,
2707 extra_var, 0, 0);
2708 if (!info->bmap)
2709 return -1;
2710 n = isl_aff_list_n_aff(list);
2711 for (i = 0; i < n; ++i) {
2712 int is_nan;
2713 isl_aff *aff;
2715 aff = isl_aff_list_get_aff(list, i);
2716 is_nan = isl_aff_is_nan(aff);
2717 isl_aff_free(aff);
2718 if (is_nan < 0)
2719 return -1;
2720 if (is_nan)
2721 continue;
2723 if (isl_tab_insert_var(info->tab, dim + i) < 0)
2724 return -1;
2725 d = isl_basic_map_alloc_div(info->bmap);
2726 if (d < 0)
2727 return -1;
2728 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
2729 if (!info->bmap)
2730 return -1;
2731 for (j = d; j > i; --j)
2732 isl_basic_map_swap_div(info->bmap, j - 1, j);
2735 return 0;
2738 /* For each element in "list" that is not set to NaN, fix the corresponding
2739 * variable in "tab" to the purely affine expression defined by the element.
2740 * "dim" is the offset in the variables of "tab" where we should
2741 * start considering the elements in "list".
2743 * This function assumes that a sufficient number of rows and
2744 * elements in the constraint array are available in the tableau.
2746 static int add_sub_equalities(struct isl_tab *tab,
2747 __isl_keep isl_aff_list *list, int dim)
2749 int i, n;
2750 isl_ctx *ctx;
2751 isl_vec *sub;
2752 isl_aff *aff;
2754 n = isl_aff_list_n_aff(list);
2756 ctx = isl_tab_get_ctx(tab);
2757 sub = isl_vec_alloc(ctx, 1 + dim + n);
2758 if (!sub)
2759 return -1;
2760 isl_seq_clr(sub->el + 1 + dim, n);
2762 for (i = 0; i < n; ++i) {
2763 aff = isl_aff_list_get_aff(list, i);
2764 if (!aff)
2765 goto error;
2766 if (isl_aff_is_nan(aff)) {
2767 isl_aff_free(aff);
2768 continue;
2770 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2771 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2772 if (isl_tab_add_eq(tab, sub->el) < 0)
2773 goto error;
2774 isl_int_set_si(sub->el[1 + dim + i], 0);
2775 isl_aff_free(aff);
2778 isl_vec_free(sub);
2779 return 0;
2780 error:
2781 isl_aff_free(aff);
2782 isl_vec_free(sub);
2783 return -1;
2786 /* Add variables to info->tab and info->bmap corresponding to the elements
2787 * in "list" that are not set to NaN. The value of the added variable
2788 * in info->tab is fixed to the purely affine expression defined by the element.
2789 * "dim" is the offset in the variables of info->tab where we should
2790 * start considering the elements in "list".
2791 * When this function returns, the total number of variables in info->tab
2792 * is equal to "dim" plus the number of elements in "list".
2794 static int add_subs(struct isl_coalesce_info *info,
2795 __isl_keep isl_aff_list *list, int dim)
2797 int extra_var;
2798 int n;
2800 if (!list)
2801 return -1;
2803 n = isl_aff_list_n_aff(list);
2804 extra_var = n - (info->tab->n_var - dim);
2806 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2807 return -1;
2808 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2809 return -1;
2810 if (add_sub_vars(info, list, dim, extra_var) < 0)
2811 return -1;
2813 return add_sub_equalities(info->tab, list, dim);
2816 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2817 * divisions in "i" but not in "j" to basic map "j", with values
2818 * specified by "list". The total number of elements in "list"
2819 * is equal to the number of integer divisions in "i", while the number
2820 * of NaN elements in the list is equal to the number of integer divisions
2821 * in "j".
2823 * If no coalescing can be performed, then we need to revert basic map "j"
2824 * to its original state. We do the same if basic map "i" gets dropped
2825 * during the coalescing, even though this should not happen in practice
2826 * since we have already checked for "j" being a subset of "i"
2827 * before we reach this stage.
2829 static enum isl_change coalesce_with_subs(int i, int j,
2830 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2832 isl_basic_map *bmap_j;
2833 struct isl_tab_undo *snap;
2834 unsigned dim;
2835 enum isl_change change;
2837 bmap_j = isl_basic_map_copy(info[j].bmap);
2838 snap = isl_tab_snap(info[j].tab);
2840 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2841 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2842 if (add_subs(&info[j], list, dim) < 0)
2843 goto error;
2845 change = coalesce_local_pair(i, j, info);
2846 if (change != isl_change_none && change != isl_change_drop_first) {
2847 isl_basic_map_free(bmap_j);
2848 } else {
2849 isl_basic_map_free(info[j].bmap);
2850 info[j].bmap = bmap_j;
2852 if (isl_tab_rollback(info[j].tab, snap) < 0)
2853 return isl_change_error;
2856 return change;
2857 error:
2858 isl_basic_map_free(bmap_j);
2859 return isl_change_error;
2862 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2863 * those extra integer divisions in "i" that can be simplified away
2864 * using the extra equalities in "j".
2865 * All divs are assumed to be known and not contain any nested divs.
2867 * We first check if there are any extra equalities in "j" that we
2868 * can exploit. Then we check if every integer division in "i"
2869 * either already appears in "j" or can be simplified using the
2870 * extra equalities to a purely affine expression.
2871 * If these tests succeed, then we try to coalesce the two basic maps
2872 * by introducing extra dimensions in "j" corresponding to
2873 * the extra integer divsisions "i" fixed to the corresponding
2874 * purely affine expression.
2876 static enum isl_change check_coalesce_into_eq(int i, int j,
2877 struct isl_coalesce_info *info)
2879 unsigned n_div_i, n_div_j;
2880 isl_basic_map *hull_i, *hull_j;
2881 int equal, empty;
2882 isl_aff_list *list;
2883 enum isl_change change;
2885 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2886 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2887 if (n_div_i <= n_div_j)
2888 return isl_change_none;
2889 if (info[j].bmap->n_eq == 0)
2890 return isl_change_none;
2892 hull_i = isl_basic_map_copy(info[i].bmap);
2893 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2894 hull_j = isl_basic_map_copy(info[j].bmap);
2895 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2897 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2898 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2899 empty = isl_basic_map_plain_is_empty(hull_j);
2900 isl_basic_map_free(hull_i);
2902 if (equal < 0 || empty < 0)
2903 goto error;
2904 if (equal || empty) {
2905 isl_basic_map_free(hull_j);
2906 return isl_change_none;
2909 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2910 if (!list)
2911 return isl_change_error;
2912 if (isl_aff_list_n_aff(list) < n_div_i)
2913 change = isl_change_none;
2914 else
2915 change = coalesce_with_subs(i, j, info, list);
2917 isl_aff_list_free(list);
2919 return change;
2920 error:
2921 isl_basic_map_free(hull_j);
2922 return isl_change_error;
2925 /* Check if we can coalesce basic maps "i" and "j" after copying
2926 * those extra integer divisions in one of the basic maps that can
2927 * be simplified away using the extra equalities in the other basic map.
2928 * We require all divs to be known in both basic maps.
2929 * Furthermore, to simplify the comparison of div expressions,
2930 * we do not allow any nested integer divisions.
2932 static enum isl_change check_coalesce_eq(int i, int j,
2933 struct isl_coalesce_info *info)
2935 int known, nested;
2936 enum isl_change change;
2938 known = isl_basic_map_divs_known(info[i].bmap);
2939 if (known < 0 || !known)
2940 return known < 0 ? isl_change_error : isl_change_none;
2941 known = isl_basic_map_divs_known(info[j].bmap);
2942 if (known < 0 || !known)
2943 return known < 0 ? isl_change_error : isl_change_none;
2944 nested = has_nested_div(info[i].bmap);
2945 if (nested < 0 || nested)
2946 return nested < 0 ? isl_change_error : isl_change_none;
2947 nested = has_nested_div(info[j].bmap);
2948 if (nested < 0 || nested)
2949 return nested < 0 ? isl_change_error : isl_change_none;
2951 change = check_coalesce_into_eq(i, j, info);
2952 if (change != isl_change_none)
2953 return change;
2954 change = check_coalesce_into_eq(j, i, info);
2955 if (change != isl_change_none)
2956 return invert_change(change);
2958 return isl_change_none;
2961 /* Check if the union of the given pair of basic maps
2962 * can be represented by a single basic map.
2963 * If so, replace the pair by the single basic map and return
2964 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2965 * Otherwise, return isl_change_none.
2967 * We first check if the two basic maps live in the same local space,
2968 * after aligning the divs that differ by only an integer constant.
2969 * If so, we do the complete check. Otherwise, we check if they have
2970 * the same number of integer divisions and can be coalesced, if one is
2971 * an obvious subset of the other or if the extra integer divisions
2972 * of one basic map can be simplified away using the extra equalities
2973 * of the other basic map.
2975 static enum isl_change coalesce_pair(int i, int j,
2976 struct isl_coalesce_info *info)
2978 int same;
2979 enum isl_change change;
2981 if (harmonize_divs(&info[i], &info[j]) < 0)
2982 return isl_change_error;
2983 same = same_divs(info[i].bmap, info[j].bmap);
2984 if (same < 0)
2985 return isl_change_error;
2986 if (same)
2987 return coalesce_local_pair(i, j, info);
2989 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2990 change = coalesce_local_pair(i, j, info);
2991 if (change != isl_change_none)
2992 return change;
2995 change = coalesce_divs(i, j, info);
2996 if (change != isl_change_none)
2997 return change;
2999 return check_coalesce_eq(i, j, info);
3002 /* Return the maximum of "a" and "b".
3004 static int isl_max(int a, int b)
3006 return a > b ? a : b;
3009 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3010 * with those in the range [start2, end2[, skipping basic maps
3011 * that have been removed (either before or within this function).
3013 * For each basic map i in the first range, we check if it can be coalesced
3014 * with respect to any previously considered basic map j in the second range.
3015 * If i gets dropped (because it was a subset of some j), then
3016 * we can move on to the next basic map.
3017 * If j gets dropped, we need to continue checking against the other
3018 * previously considered basic maps.
3019 * If the two basic maps got fused, then we recheck the fused basic map
3020 * against the previously considered basic maps, starting at i + 1
3021 * (even if start2 is greater than i + 1).
3023 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3024 int start1, int end1, int start2, int end2)
3026 int i, j;
3028 for (i = end1 - 1; i >= start1; --i) {
3029 if (info[i].removed)
3030 continue;
3031 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3032 enum isl_change changed;
3034 if (info[j].removed)
3035 continue;
3036 if (info[i].removed)
3037 isl_die(ctx, isl_error_internal,
3038 "basic map unexpectedly removed",
3039 return -1);
3040 changed = coalesce_pair(i, j, info);
3041 switch (changed) {
3042 case isl_change_error:
3043 return -1;
3044 case isl_change_none:
3045 case isl_change_drop_second:
3046 continue;
3047 case isl_change_drop_first:
3048 j = end2;
3049 break;
3050 case isl_change_fuse:
3051 j = i;
3052 break;
3057 return 0;
3060 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3062 * We consider groups of basic maps that live in the same apparent
3063 * affine hull and we first coalesce within such a group before we
3064 * coalesce the elements in the group with elements of previously
3065 * considered groups. If a fuse happens during the second phase,
3066 * then we also reconsider the elements within the group.
3068 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3070 int start, end;
3072 for (end = n; end > 0; end = start) {
3073 start = end - 1;
3074 while (start >= 1 &&
3075 info[start - 1].hull_hash == info[start].hull_hash)
3076 start--;
3077 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3078 return -1;
3079 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3080 return -1;
3083 return 0;
3086 /* Update the basic maps in "map" based on the information in "info".
3087 * In particular, remove the basic maps that have been marked removed and
3088 * update the others based on the information in the corresponding tableau.
3089 * Since we detected implicit equalities without calling
3090 * isl_basic_map_gauss, we need to do it now.
3091 * Also call isl_basic_map_simplify if we may have lost the definition
3092 * of one or more integer divisions.
3094 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3095 int n, struct isl_coalesce_info *info)
3097 int i;
3099 if (!map)
3100 return NULL;
3102 for (i = n - 1; i >= 0; --i) {
3103 if (info[i].removed) {
3104 isl_basic_map_free(map->p[i]);
3105 if (i != map->n - 1)
3106 map->p[i] = map->p[map->n - 1];
3107 map->n--;
3108 continue;
3111 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3112 info[i].tab);
3113 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3114 if (info[i].simplify)
3115 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3116 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3117 if (!info[i].bmap)
3118 return isl_map_free(map);
3119 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3120 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3121 isl_basic_map_free(map->p[i]);
3122 map->p[i] = info[i].bmap;
3123 info[i].bmap = NULL;
3126 return map;
3129 /* For each pair of basic maps in the map, check if the union of the two
3130 * can be represented by a single basic map.
3131 * If so, replace the pair by the single basic map and start over.
3133 * We factor out any (hidden) common factor from the constraint
3134 * coefficients to improve the detection of adjacent constraints.
3136 * Since we are constructing the tableaus of the basic maps anyway,
3137 * we exploit them to detect implicit equalities and redundant constraints.
3138 * This also helps the coalescing as it can ignore the redundant constraints.
3139 * In order to avoid confusion, we make all implicit equalities explicit
3140 * in the basic maps. We don't call isl_basic_map_gauss, though,
3141 * as that may affect the number of constraints.
3142 * This means that we have to call isl_basic_map_gauss at the end
3143 * of the computation (in update_basic_maps) to ensure that
3144 * the basic maps are not left in an unexpected state.
3145 * For each basic map, we also compute the hash of the apparent affine hull
3146 * for use in coalesce.
3148 struct isl_map *isl_map_coalesce(struct isl_map *map)
3150 int i;
3151 unsigned n;
3152 isl_ctx *ctx;
3153 struct isl_coalesce_info *info = NULL;
3155 map = isl_map_remove_empty_parts(map);
3156 if (!map)
3157 return NULL;
3159 if (map->n <= 1)
3160 return map;
3162 ctx = isl_map_get_ctx(map);
3163 map = isl_map_sort_divs(map);
3164 map = isl_map_cow(map);
3166 if (!map)
3167 return NULL;
3169 n = map->n;
3171 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3172 if (!info)
3173 goto error;
3175 for (i = 0; i < map->n; ++i) {
3176 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3177 if (!map->p[i])
3178 goto error;
3179 info[i].bmap = isl_basic_map_copy(map->p[i]);
3180 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3181 if (!info[i].tab)
3182 goto error;
3183 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3184 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3185 goto error;
3186 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3187 info[i].bmap);
3188 if (!info[i].bmap)
3189 goto error;
3190 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3191 if (isl_tab_detect_redundant(info[i].tab) < 0)
3192 goto error;
3193 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3194 goto error;
3196 for (i = map->n - 1; i >= 0; --i)
3197 if (info[i].tab->empty)
3198 drop(&info[i]);
3200 if (coalesce(ctx, n, info) < 0)
3201 goto error;
3203 map = update_basic_maps(map, n, info);
3205 clear_coalesce_info(n, info);
3207 return map;
3208 error:
3209 clear_coalesce_info(n, info);
3210 isl_map_free(map);
3211 return NULL;
3214 /* For each pair of basic sets in the set, check if the union of the two
3215 * can be represented by a single basic set.
3216 * If so, replace the pair by the single basic set and start over.
3218 struct isl_set *isl_set_coalesce(struct isl_set *set)
3220 return set_from_map(isl_map_coalesce(set_to_map(set)));