isl_tab_pip.c: set free callback before any other fields
[isl.git] / isl_coalesce.c
blobff7672afbb7168b32e824413c0b4e95d2199c5bf
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
18 * CS 42112, 75589 Paris Cedex 12, France
21 #include <isl_ctx_private.h>
22 #include "isl_map_private.h"
23 #include <isl_seq.h>
24 #include <isl/options.h>
25 #include "isl_tab.h"
26 #include <isl_mat_private.h>
27 #include <isl_local_space_private.h>
28 #include <isl_val_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_equalities.h>
32 #include <isl_constraint_private.h>
34 #include <set_to_map.c>
35 #include <set_from_map.c>
37 #define STATUS_ERROR -1
38 #define STATUS_REDUNDANT 1
39 #define STATUS_VALID 2
40 #define STATUS_SEPARATE 3
41 #define STATUS_CUT 4
42 #define STATUS_ADJ_EQ 5
43 #define STATUS_ADJ_INEQ 6
45 static int status_in(isl_int *ineq, struct isl_tab *tab)
47 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
48 switch (type) {
49 default:
50 case isl_ineq_error: return STATUS_ERROR;
51 case isl_ineq_redundant: return STATUS_VALID;
52 case isl_ineq_separate: return STATUS_SEPARATE;
53 case isl_ineq_cut: return STATUS_CUT;
54 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
55 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
59 /* Compute the position of the equalities of basic map "bmap_i"
60 * with respect to the basic map represented by "tab_j".
61 * The resulting array has twice as many entries as the number
62 * of equalities corresponding to the two inequalties to which
63 * each equality corresponds.
65 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
66 struct isl_tab *tab_j)
68 int k, l;
69 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
70 unsigned dim;
72 if (!eq)
73 return NULL;
75 dim = isl_basic_map_total_dim(bmap_i);
76 for (k = 0; k < bmap_i->n_eq; ++k) {
77 for (l = 0; l < 2; ++l) {
78 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
79 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
80 if (eq[2 * k + l] == STATUS_ERROR)
81 goto error;
85 return eq;
86 error:
87 free(eq);
88 return NULL;
91 /* Compute the position of the inequalities of basic map "bmap_i"
92 * (also represented by "tab_i", if not NULL) with respect to the basic map
93 * represented by "tab_j".
95 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
96 struct isl_tab *tab_i, struct isl_tab *tab_j)
98 int k;
99 unsigned n_eq = bmap_i->n_eq;
100 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
102 if (!ineq)
103 return NULL;
105 for (k = 0; k < bmap_i->n_ineq; ++k) {
106 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
107 ineq[k] = STATUS_REDUNDANT;
108 continue;
110 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
111 if (ineq[k] == STATUS_ERROR)
112 goto error;
113 if (ineq[k] == STATUS_SEPARATE)
114 break;
117 return ineq;
118 error:
119 free(ineq);
120 return NULL;
123 static int any(int *con, unsigned len, int status)
125 int i;
127 for (i = 0; i < len ; ++i)
128 if (con[i] == status)
129 return 1;
130 return 0;
133 /* Return the first position of "status" in the list "con" of length "len".
134 * Return -1 if there is no such entry.
136 static int find(int *con, unsigned len, int status)
138 int i;
140 for (i = 0; i < len ; ++i)
141 if (con[i] == status)
142 return i;
143 return -1;
146 static int count(int *con, unsigned len, int status)
148 int i;
149 int c = 0;
151 for (i = 0; i < len ; ++i)
152 if (con[i] == status)
153 c++;
154 return c;
157 static int all(int *con, unsigned len, int status)
159 int i;
161 for (i = 0; i < len ; ++i) {
162 if (con[i] == STATUS_REDUNDANT)
163 continue;
164 if (con[i] != status)
165 return 0;
167 return 1;
170 /* Internal information associated to a basic map in a map
171 * that is to be coalesced by isl_map_coalesce.
173 * "bmap" is the basic map itself (or NULL if "removed" is set)
174 * "tab" is the corresponding tableau (or NULL if "removed" is set)
175 * "hull_hash" identifies the affine space in which "bmap" lives.
176 * "removed" is set if this basic map has been removed from the map
177 * "simplify" is set if this basic map may have some unknown integer
178 * divisions that were not present in the input basic maps. The basic
179 * map should then be simplified such that we may be able to find
180 * a definition among the constraints.
182 * "eq" and "ineq" are only set if we are currently trying to coalesce
183 * this basic map with another basic map, in which case they represent
184 * the position of the inequalities of this basic map with respect to
185 * the other basic map. The number of elements in the "eq" array
186 * is twice the number of equalities in the "bmap", corresponding
187 * to the two inequalities that make up each equality.
189 struct isl_coalesce_info {
190 isl_basic_map *bmap;
191 struct isl_tab *tab;
192 uint32_t hull_hash;
193 int removed;
194 int simplify;
195 int *eq;
196 int *ineq;
199 /* Are all non-redundant constraints of the basic map represented by "info"
200 * either valid or cut constraints with respect to the other basic map?
202 static int all_valid_or_cut(struct isl_coalesce_info *info)
204 int i;
206 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
207 if (info->eq[i] == STATUS_REDUNDANT)
208 continue;
209 if (info->eq[i] == STATUS_VALID)
210 continue;
211 if (info->eq[i] == STATUS_CUT)
212 continue;
213 return 0;
216 for (i = 0; i < info->bmap->n_ineq; ++i) {
217 if (info->ineq[i] == STATUS_REDUNDANT)
218 continue;
219 if (info->ineq[i] == STATUS_VALID)
220 continue;
221 if (info->ineq[i] == STATUS_CUT)
222 continue;
223 return 0;
226 return 1;
229 /* Compute the hash of the (apparent) affine hull of info->bmap (with
230 * the existentially quantified variables removed) and store it
231 * in info->hash.
233 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
235 isl_basic_map *hull;
236 unsigned n_div;
238 hull = isl_basic_map_copy(info->bmap);
239 hull = isl_basic_map_plain_affine_hull(hull);
240 n_div = isl_basic_map_dim(hull, isl_dim_div);
241 hull = isl_basic_map_drop_constraints_involving_dims(hull,
242 isl_dim_div, 0, n_div);
243 info->hull_hash = isl_basic_map_get_hash(hull);
244 isl_basic_map_free(hull);
246 return hull ? 0 : -1;
249 /* Free all the allocated memory in an array
250 * of "n" isl_coalesce_info elements.
252 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
254 int i;
256 if (!info)
257 return;
259 for (i = 0; i < n; ++i) {
260 isl_basic_map_free(info[i].bmap);
261 isl_tab_free(info[i].tab);
264 free(info);
267 /* Drop the basic map represented by "info".
268 * That is, clear the memory associated to the entry and
269 * mark it as having been removed.
271 static void drop(struct isl_coalesce_info *info)
273 info->bmap = isl_basic_map_free(info->bmap);
274 isl_tab_free(info->tab);
275 info->tab = NULL;
276 info->removed = 1;
279 /* Exchange the information in "info1" with that in "info2".
281 static void exchange(struct isl_coalesce_info *info1,
282 struct isl_coalesce_info *info2)
284 struct isl_coalesce_info info;
286 info = *info1;
287 *info1 = *info2;
288 *info2 = info;
291 /* This type represents the kind of change that has been performed
292 * while trying to coalesce two basic maps.
294 * isl_change_none: nothing was changed
295 * isl_change_drop_first: the first basic map was removed
296 * isl_change_drop_second: the second basic map was removed
297 * isl_change_fuse: the two basic maps were replaced by a new basic map.
299 enum isl_change {
300 isl_change_error = -1,
301 isl_change_none = 0,
302 isl_change_drop_first,
303 isl_change_drop_second,
304 isl_change_fuse,
307 /* Update "change" based on an interchange of the first and the second
308 * basic map. That is, interchange isl_change_drop_first and
309 * isl_change_drop_second.
311 static enum isl_change invert_change(enum isl_change change)
313 switch (change) {
314 case isl_change_error:
315 return isl_change_error;
316 case isl_change_none:
317 return isl_change_none;
318 case isl_change_drop_first:
319 return isl_change_drop_second;
320 case isl_change_drop_second:
321 return isl_change_drop_first;
322 case isl_change_fuse:
323 return isl_change_fuse;
326 return isl_change_error;
329 /* Add the valid constraints of the basic map represented by "info"
330 * to "bmap". "len" is the size of the constraints.
331 * If only one of the pair of inequalities that make up an equality
332 * is valid, then add that inequality.
334 static __isl_give isl_basic_map *add_valid_constraints(
335 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
336 unsigned len)
338 int k, l;
340 if (!bmap)
341 return NULL;
343 for (k = 0; k < info->bmap->n_eq; ++k) {
344 if (info->eq[2 * k] == STATUS_VALID &&
345 info->eq[2 * k + 1] == STATUS_VALID) {
346 l = isl_basic_map_alloc_equality(bmap);
347 if (l < 0)
348 return isl_basic_map_free(bmap);
349 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
350 } else if (info->eq[2 * k] == STATUS_VALID) {
351 l = isl_basic_map_alloc_inequality(bmap);
352 if (l < 0)
353 return isl_basic_map_free(bmap);
354 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
355 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
356 l = isl_basic_map_alloc_inequality(bmap);
357 if (l < 0)
358 return isl_basic_map_free(bmap);
359 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
363 for (k = 0; k < info->bmap->n_ineq; ++k) {
364 if (info->ineq[k] != STATUS_VALID)
365 continue;
366 l = isl_basic_map_alloc_inequality(bmap);
367 if (l < 0)
368 return isl_basic_map_free(bmap);
369 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
372 return bmap;
375 /* Is "bmap" defined by a number of (non-redundant) constraints that
376 * is greater than the number of constraints of basic maps i and j combined?
377 * Equalities are counted as two inequalities.
379 static int number_of_constraints_increases(int i, int j,
380 struct isl_coalesce_info *info,
381 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
383 int k, n_old, n_new;
385 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
386 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
388 n_new = 2 * bmap->n_eq;
389 for (k = 0; k < bmap->n_ineq; ++k)
390 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
391 ++n_new;
393 return n_new > n_old;
396 /* Replace the pair of basic maps i and j by the basic map bounded
397 * by the valid constraints in both basic maps and the constraints
398 * in extra (if not NULL).
399 * Place the fused basic map in the position that is the smallest of i and j.
401 * If "detect_equalities" is set, then look for equalities encoded
402 * as pairs of inequalities.
403 * If "check_number" is set, then the original basic maps are only
404 * replaced if the total number of constraints does not increase.
405 * While the number of integer divisions in the two basic maps
406 * is assumed to be the same, the actual definitions may be different.
407 * We only copy the definition from one of the basic map if it is
408 * the same as that of the other basic map. Otherwise, we mark
409 * the integer division as unknown and simplify the basic map
410 * in an attempt to recover the integer division definition.
412 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
413 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
415 int k, l;
416 struct isl_basic_map *fused = NULL;
417 struct isl_tab *fused_tab = NULL;
418 unsigned total = isl_basic_map_total_dim(info[i].bmap);
419 unsigned extra_rows = extra ? extra->n_row : 0;
420 unsigned n_eq, n_ineq;
421 int simplify = 0;
423 if (j < i)
424 return fuse(j, i, info, extra, detect_equalities, check_number);
426 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
427 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
428 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
429 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
430 fused = add_valid_constraints(fused, &info[i], 1 + total);
431 fused = add_valid_constraints(fused, &info[j], 1 + total);
432 if (!fused)
433 goto error;
434 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
435 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
436 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
438 for (k = 0; k < info[i].bmap->n_div; ++k) {
439 int l = isl_basic_map_alloc_div(fused);
440 if (l < 0)
441 goto error;
442 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
443 1 + 1 + total)) {
444 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
445 1 + 1 + total);
446 } else {
447 isl_int_set_si(fused->div[l][0], 0);
448 simplify = 1;
452 for (k = 0; k < extra_rows; ++k) {
453 l = isl_basic_map_alloc_inequality(fused);
454 if (l < 0)
455 goto error;
456 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
459 if (detect_equalities)
460 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
461 fused = isl_basic_map_gauss(fused, NULL);
462 if (simplify || info[j].simplify) {
463 fused = isl_basic_map_simplify(fused);
464 info[i].simplify = 0;
466 fused = isl_basic_map_finalize(fused);
468 fused_tab = isl_tab_from_basic_map(fused, 0);
469 if (isl_tab_detect_redundant(fused_tab) < 0)
470 goto error;
472 if (check_number &&
473 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
474 isl_tab_free(fused_tab);
475 isl_basic_map_free(fused);
476 return isl_change_none;
479 isl_basic_map_free(info[i].bmap);
480 info[i].bmap = fused;
481 isl_tab_free(info[i].tab);
482 info[i].tab = fused_tab;
483 drop(&info[j]);
485 return isl_change_fuse;
486 error:
487 isl_tab_free(fused_tab);
488 isl_basic_map_free(fused);
489 return isl_change_error;
492 /* Given a pair of basic maps i and j such that all constraints are either
493 * "valid" or "cut", check if the facets corresponding to the "cut"
494 * constraints of i lie entirely within basic map j.
495 * If so, replace the pair by the basic map consisting of the valid
496 * constraints in both basic maps.
497 * Checking whether the facet lies entirely within basic map j
498 * is performed by checking whether the constraints of basic map j
499 * are valid for the facet. These tests are performed on a rational
500 * tableau to avoid the theoretical possibility that a constraint
501 * that was considered to be a cut constraint for the entire basic map i
502 * happens to be considered to be a valid constraint for the facet,
503 * even though it cuts off the same rational points.
505 * To see that we are not introducing any extra points, call the
506 * two basic maps A and B and the resulting map U and let x
507 * be an element of U \setminus ( A \cup B ).
508 * A line connecting x with an element of A \cup B meets a facet F
509 * of either A or B. Assume it is a facet of B and let c_1 be
510 * the corresponding facet constraint. We have c_1(x) < 0 and
511 * so c_1 is a cut constraint. This implies that there is some
512 * (possibly rational) point x' satisfying the constraints of A
513 * and the opposite of c_1 as otherwise c_1 would have been marked
514 * valid for A. The line connecting x and x' meets a facet of A
515 * in a (possibly rational) point that also violates c_1, but this
516 * is impossible since all cut constraints of B are valid for all
517 * cut facets of A.
518 * In case F is a facet of A rather than B, then we can apply the
519 * above reasoning to find a facet of B separating x from A \cup B first.
521 static enum isl_change check_facets(int i, int j,
522 struct isl_coalesce_info *info)
524 int k, l;
525 struct isl_tab_undo *snap, *snap2;
526 unsigned n_eq = info[i].bmap->n_eq;
528 snap = isl_tab_snap(info[i].tab);
529 if (isl_tab_mark_rational(info[i].tab) < 0)
530 return isl_change_error;
531 snap2 = isl_tab_snap(info[i].tab);
533 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
534 if (info[i].ineq[k] != STATUS_CUT)
535 continue;
536 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
537 return isl_change_error;
538 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
539 int stat;
540 if (info[j].ineq[l] != STATUS_CUT)
541 continue;
542 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
543 if (stat < 0)
544 return isl_change_error;
545 if (stat != STATUS_VALID)
546 break;
548 if (isl_tab_rollback(info[i].tab, snap2) < 0)
549 return isl_change_error;
550 if (l < info[j].bmap->n_ineq)
551 break;
554 if (k < info[i].bmap->n_ineq) {
555 if (isl_tab_rollback(info[i].tab, snap) < 0)
556 return isl_change_error;
557 return isl_change_none;
559 return fuse(i, j, info, NULL, 0, 0);
562 /* Check if info->bmap contains the basic map represented
563 * by the tableau "tab".
564 * For each equality, we check both the constraint itself
565 * (as an inequality) and its negation. Make sure the
566 * equality is returned to its original state before returning.
568 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
570 int k;
571 unsigned dim;
572 isl_basic_map *bmap = info->bmap;
574 dim = isl_basic_map_total_dim(bmap);
575 for (k = 0; k < bmap->n_eq; ++k) {
576 int stat;
577 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
578 stat = status_in(bmap->eq[k], tab);
579 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
580 if (stat < 0)
581 return -1;
582 if (stat != STATUS_VALID)
583 return 0;
584 stat = status_in(bmap->eq[k], tab);
585 if (stat < 0)
586 return -1;
587 if (stat != STATUS_VALID)
588 return 0;
591 for (k = 0; k < bmap->n_ineq; ++k) {
592 int stat;
593 if (info->ineq[k] == STATUS_REDUNDANT)
594 continue;
595 stat = status_in(bmap->ineq[k], tab);
596 if (stat < 0)
597 return -1;
598 if (stat != STATUS_VALID)
599 return 0;
601 return 1;
604 /* Basic map "i" has an inequality (say "k") that is adjacent
605 * to some inequality of basic map "j". All the other inequalities
606 * are valid for "j".
607 * Check if basic map "j" forms an extension of basic map "i".
609 * Note that this function is only called if some of the equalities or
610 * inequalities of basic map "j" do cut basic map "i". The function is
611 * correct even if there are no such cut constraints, but in that case
612 * the additional checks performed by this function are overkill.
614 * In particular, we replace constraint k, say f >= 0, by constraint
615 * f <= -1, add the inequalities of "j" that are valid for "i"
616 * and check if the result is a subset of basic map "j".
617 * To improve the chances of the subset relation being detected,
618 * any variable that only attains a single integer value
619 * in the tableau of "i" is first fixed to that value.
620 * If the result is a subset, then we know that this result is exactly equal
621 * to basic map "j" since all its constraints are valid for basic map "j".
622 * By combining the valid constraints of "i" (all equalities and all
623 * inequalities except "k") and the valid constraints of "j" we therefore
624 * obtain a basic map that is equal to their union.
625 * In this case, there is no need to perform a rollback of the tableau
626 * since it is going to be destroyed in fuse().
629 * |\__ |\__
630 * | \__ | \__
631 * | \_ => | \__
632 * |_______| _ |_________\
635 * |\ |\
636 * | \ | \
637 * | \ | \
638 * | | | \
639 * | ||\ => | \
640 * | || \ | \
641 * | || | | |
642 * |__||_/ |_____/
644 static enum isl_change is_adj_ineq_extension(int i, int j,
645 struct isl_coalesce_info *info)
647 int k;
648 struct isl_tab_undo *snap;
649 unsigned n_eq = info[i].bmap->n_eq;
650 unsigned total = isl_basic_map_total_dim(info[i].bmap);
651 int r;
652 int super;
654 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
655 return isl_change_error;
657 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
658 if (k < 0)
659 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
660 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
661 return isl_change_error);
663 snap = isl_tab_snap(info[i].tab);
665 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
666 return isl_change_error;
668 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
669 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
670 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
671 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
672 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
673 if (r < 0)
674 return isl_change_error;
676 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
677 if (info[j].ineq[k] != STATUS_VALID)
678 continue;
679 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
680 return isl_change_error;
682 if (isl_tab_detect_constants(info[i].tab) < 0)
683 return isl_change_error;
685 super = contains(&info[j], info[i].tab);
686 if (super < 0)
687 return isl_change_error;
688 if (super)
689 return fuse(i, j, info, NULL, 0, 0);
691 if (isl_tab_rollback(info[i].tab, snap) < 0)
692 return isl_change_error;
694 return isl_change_none;
698 /* Both basic maps have at least one inequality with and adjacent
699 * (but opposite) inequality in the other basic map.
700 * Check that there are no cut constraints and that there is only
701 * a single pair of adjacent inequalities.
702 * If so, we can replace the pair by a single basic map described
703 * by all but the pair of adjacent inequalities.
704 * Any additional points introduced lie strictly between the two
705 * adjacent hyperplanes and can therefore be integral.
707 * ____ _____
708 * / ||\ / \
709 * / || \ / \
710 * \ || \ => \ \
711 * \ || / \ /
712 * \___||_/ \_____/
714 * The test for a single pair of adjancent inequalities is important
715 * for avoiding the combination of two basic maps like the following
717 * /|
718 * / |
719 * /__|
720 * _____
721 * | |
722 * | |
723 * |___|
725 * If there are some cut constraints on one side, then we may
726 * still be able to fuse the two basic maps, but we need to perform
727 * some additional checks in is_adj_ineq_extension.
729 static enum isl_change check_adj_ineq(int i, int j,
730 struct isl_coalesce_info *info)
732 int count_i, count_j;
733 int cut_i, cut_j;
735 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
736 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
738 if (count_i != 1 && count_j != 1)
739 return isl_change_none;
741 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
742 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
743 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
744 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
746 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
747 return fuse(i, j, info, NULL, 0, 0);
749 if (count_i == 1 && !cut_i)
750 return is_adj_ineq_extension(i, j, info);
752 if (count_j == 1 && !cut_j)
753 return is_adj_ineq_extension(j, i, info);
755 return isl_change_none;
758 /* Given an affine transformation matrix "T", does row "row" represent
759 * anything other than a unit vector (possibly shifted by a constant)
760 * that is not involved in any of the other rows?
762 * That is, if a constraint involves the variable corresponding to
763 * the row, then could its preimage by "T" have any coefficients
764 * that are different from those in the original constraint?
766 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
768 int i, j;
769 int len = T->n_col - 1;
771 i = isl_seq_first_non_zero(T->row[row] + 1, len);
772 if (i < 0)
773 return 1;
774 if (!isl_int_is_one(T->row[row][1 + i]) &&
775 !isl_int_is_negone(T->row[row][1 + i]))
776 return 1;
778 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
779 if (j >= 0)
780 return 1;
782 for (j = 1; j < T->n_row; ++j) {
783 if (j == row)
784 continue;
785 if (!isl_int_is_zero(T->row[j][1 + i]))
786 return 1;
789 return 0;
792 /* Does inequality constraint "ineq" of "bmap" involve any of
793 * the variables marked in "affected"?
794 * "total" is the total number of variables, i.e., the number
795 * of entries in "affected".
797 static int is_affected(__isl_keep isl_basic_map *bmap, int ineq, int *affected,
798 int total)
800 int i;
802 for (i = 0; i < total; ++i) {
803 if (!affected[i])
804 continue;
805 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
806 return 1;
809 return 0;
812 /* Given the compressed version of inequality constraint "ineq"
813 * of info->bmap in "v", check if the constraint can be tightened,
814 * where the compression is based on an equality constraint valid
815 * for info->tab.
816 * If so, add the tightened version of the inequality constraint
817 * to info->tab. "v" may be modified by this function.
819 * That is, if the compressed constraint is of the form
821 * m f() + c >= 0
823 * with 0 < c < m, then it is equivalent to
825 * f() >= 0
827 * This means that c can also be subtracted from the original,
828 * uncompressed constraint without affecting the integer points
829 * in info->tab. Add this tightened constraint as an extra row
830 * to info->tab to make this information explicitly available.
832 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
833 int ineq, __isl_take isl_vec *v)
835 isl_ctx *ctx;
836 int r;
838 if (!v)
839 return NULL;
841 ctx = isl_vec_get_ctx(v);
842 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
843 if (isl_int_is_zero(ctx->normalize_gcd) ||
844 isl_int_is_one(ctx->normalize_gcd)) {
845 return v;
848 v = isl_vec_cow(v);
849 if (!v)
850 return NULL;
852 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
853 if (isl_int_is_zero(v->el[0]))
854 return v;
856 if (isl_tab_extend_cons(info->tab, 1) < 0)
857 return isl_vec_free(v);
859 isl_int_sub(info->bmap->ineq[ineq][0],
860 info->bmap->ineq[ineq][0], v->el[0]);
861 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
862 isl_int_add(info->bmap->ineq[ineq][0],
863 info->bmap->ineq[ineq][0], v->el[0]);
865 if (r < 0)
866 return isl_vec_free(v);
868 return v;
871 /* Tighten the (non-redundant) constraints on the facet represented
872 * by info->tab.
873 * In particular, on input, info->tab represents the result
874 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
875 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
876 * replacing the one at index "l" by the corresponding equality,
877 * i.e., f_k + 1 = 0, with k = relaxed[l].
879 * Compute a variable compression from the equality constraint f_k + 1 = 0
880 * and use it to tighten the other constraints of info->bmap
881 * (that is, all constraints that have not been relaxed),
882 * updating info->tab (and leaving info->bmap untouched).
883 * The compression handles essentially two cases, one where a variable
884 * is assigned a fixed value and can therefore be eliminated, and one
885 * where one variable is a shifted multiple of some other variable and
886 * can therefore be replaced by that multiple.
887 * Gaussian elimination would also work for the first case, but for
888 * the second case, the effectiveness would depend on the order
889 * of the variables.
890 * After compression, some of the constraints may have coefficients
891 * with a common divisor. If this divisor does not divide the constant
892 * term, then the constraint can be tightened.
893 * The tightening is performed on the tableau info->tab by introducing
894 * extra (temporary) constraints.
896 * Only constraints that are possibly affected by the compression are
897 * considered. In particular, if the constraint only involves variables
898 * that are directly mapped to a distinct set of other variables, then
899 * no common divisor can be introduced and no tightening can occur.
901 * It is important to only consider the non-redundant constraints
902 * since the facet constraint has been relaxed prior to the call
903 * to this function, meaning that the constraints that were redundant
904 * prior to the relaxation may no longer be redundant.
905 * These constraints will be ignored in the fused result, so
906 * the fusion detection should not exploit them.
908 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
909 int n, int *relaxed, int l)
911 unsigned total;
912 isl_ctx *ctx;
913 isl_vec *v = NULL;
914 isl_mat *T;
915 int i;
916 int k;
917 int *affected;
919 k = relaxed[l];
920 ctx = isl_basic_map_get_ctx(info->bmap);
921 total = isl_basic_map_total_dim(info->bmap);
922 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
923 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
924 T = isl_mat_variable_compression(T, NULL);
925 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
926 if (!T)
927 return isl_stat_error;
928 if (T->n_col == 0) {
929 isl_mat_free(T);
930 return isl_stat_ok;
933 affected = isl_alloc_array(ctx, int, total);
934 if (!affected)
935 goto error;
937 for (i = 0; i < total; ++i)
938 affected[i] = not_unique_unit_row(T, 1 + i);
940 for (i = 0; i < info->bmap->n_ineq; ++i) {
941 if (any(relaxed, n, i))
942 continue;
943 if (info->ineq[i] == STATUS_REDUNDANT)
944 continue;
945 if (!is_affected(info->bmap, i, affected, total))
946 continue;
947 v = isl_vec_alloc(ctx, 1 + total);
948 if (!v)
949 goto error;
950 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
951 v = isl_vec_mat_product(v, isl_mat_copy(T));
952 v = try_tightening(info, i, v);
953 isl_vec_free(v);
954 if (!v)
955 goto error;
958 isl_mat_free(T);
959 free(affected);
960 return isl_stat_ok;
961 error:
962 isl_mat_free(T);
963 free(affected);
964 return isl_stat_error;
967 /* Replace the basic maps "i" and "j" by an extension of "i"
968 * along the "n" inequality constraints in "relax" by one.
969 * The tableau info[i].tab has already been extended.
970 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
971 * by one.
972 * Each integer division that does not have exactly the same
973 * definition in "i" and "j" is marked unknown and the basic map
974 * is scheduled to be simplified in an attempt to recover
975 * the integer division definition.
976 * Place the extension in the position that is the smallest of i and j.
978 static enum isl_change extend(int i, int j, int n, int *relax,
979 struct isl_coalesce_info *info)
981 int l;
982 unsigned total;
984 info[i].bmap = isl_basic_map_cow(info[i].bmap);
985 if (!info[i].bmap)
986 return isl_change_error;
987 total = isl_basic_map_total_dim(info[i].bmap);
988 for (l = 0; l < info[i].bmap->n_div; ++l)
989 if (!isl_seq_eq(info[i].bmap->div[l],
990 info[j].bmap->div[l], 1 + 1 + total)) {
991 isl_int_set_si(info[i].bmap->div[l][0], 0);
992 info[i].simplify = 1;
994 for (l = 0; l < n; ++l)
995 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
996 info[i].bmap->ineq[relax[l]][0], 1);
997 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
998 drop(&info[j]);
999 if (j < i)
1000 exchange(&info[i], &info[j]);
1001 return isl_change_fuse;
1004 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1005 * that are such that they include basic map "j" if they are relaxed
1006 * by one. All the other inequalities are valid for "j".
1007 * Check if basic map "j" forms an extension of basic map "i".
1009 * In particular, relax the constraints in "relax", compute the corresponding
1010 * facets one by one and check whether each of these is included
1011 * in the other basic map.
1012 * Before testing for inclusion, the constraints on each facet
1013 * are tightened to increase the chance of an inclusion being detected.
1014 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1015 * in is_adj_ineq_extension, may further increase those chances, but this
1016 * is not currently done.)
1017 * If each facet is included, we know that relaxing the constraints extends
1018 * the basic map with exactly the other basic map (we already know that this
1019 * other basic map is included in the extension, because all other
1020 * inequality constraints are valid of "j") and we can replace the
1021 * two basic maps by this extension.
1022 * ____ _____
1023 * / || / |
1024 * / || / |
1025 * \ || => \ |
1026 * \ || \ |
1027 * \___|| \____|
1030 * \ |\
1031 * |\\ | \
1032 * | \\ | \
1033 * | | => | /
1034 * | / | /
1035 * |/ |/
1037 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1038 struct isl_coalesce_info *info)
1040 int l;
1041 int super;
1042 struct isl_tab_undo *snap, *snap2;
1043 unsigned n_eq = info[i].bmap->n_eq;
1045 for (l = 0; l < n; ++l)
1046 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1047 return isl_change_none;
1049 snap = isl_tab_snap(info[i].tab);
1050 for (l = 0; l < n; ++l)
1051 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1052 return isl_change_error;
1053 snap2 = isl_tab_snap(info[i].tab);
1054 for (l = 0; l < n; ++l) {
1055 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1056 return isl_change_error;
1057 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1058 return isl_change_error;
1059 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1060 return isl_change_error;
1061 super = contains(&info[j], info[i].tab);
1062 if (super < 0)
1063 return isl_change_error;
1064 if (super)
1065 continue;
1066 if (isl_tab_rollback(info[i].tab, snap) < 0)
1067 return isl_change_error;
1068 return isl_change_none;
1071 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1072 return isl_change_error;
1073 return extend(i, j, n, relax, info);
1076 /* Data structure that keeps track of the wrapping constraints
1077 * and of information to bound the coefficients of those constraints.
1079 * bound is set if we want to apply a bound on the coefficients
1080 * mat contains the wrapping constraints
1081 * max is the bound on the coefficients (if bound is set)
1083 struct isl_wraps {
1084 int bound;
1085 isl_mat *mat;
1086 isl_int max;
1089 /* Update wraps->max to be greater than or equal to the coefficients
1090 * in the equalities and inequalities of info->bmap that can be removed
1091 * if we end up applying wrapping.
1093 static void wraps_update_max(struct isl_wraps *wraps,
1094 struct isl_coalesce_info *info)
1096 int k;
1097 isl_int max_k;
1098 unsigned total = isl_basic_map_total_dim(info->bmap);
1100 isl_int_init(max_k);
1102 for (k = 0; k < info->bmap->n_eq; ++k) {
1103 if (info->eq[2 * k] == STATUS_VALID &&
1104 info->eq[2 * k + 1] == STATUS_VALID)
1105 continue;
1106 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1107 if (isl_int_abs_gt(max_k, wraps->max))
1108 isl_int_set(wraps->max, max_k);
1111 for (k = 0; k < info->bmap->n_ineq; ++k) {
1112 if (info->ineq[k] == STATUS_VALID ||
1113 info->ineq[k] == STATUS_REDUNDANT)
1114 continue;
1115 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1116 if (isl_int_abs_gt(max_k, wraps->max))
1117 isl_int_set(wraps->max, max_k);
1120 isl_int_clear(max_k);
1123 /* Initialize the isl_wraps data structure.
1124 * If we want to bound the coefficients of the wrapping constraints,
1125 * we set wraps->max to the largest coefficient
1126 * in the equalities and inequalities that can be removed if we end up
1127 * applying wrapping.
1129 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1130 struct isl_coalesce_info *info, int i, int j)
1132 isl_ctx *ctx;
1134 wraps->bound = 0;
1135 wraps->mat = mat;
1136 if (!mat)
1137 return;
1138 ctx = isl_mat_get_ctx(mat);
1139 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1140 if (!wraps->bound)
1141 return;
1142 isl_int_init(wraps->max);
1143 isl_int_set_si(wraps->max, 0);
1144 wraps_update_max(wraps, &info[i]);
1145 wraps_update_max(wraps, &info[j]);
1148 /* Free the contents of the isl_wraps data structure.
1150 static void wraps_free(struct isl_wraps *wraps)
1152 isl_mat_free(wraps->mat);
1153 if (wraps->bound)
1154 isl_int_clear(wraps->max);
1157 /* Is the wrapping constraint in row "row" allowed?
1159 * If wraps->bound is set, we check that none of the coefficients
1160 * is greater than wraps->max.
1162 static int allow_wrap(struct isl_wraps *wraps, int row)
1164 int i;
1166 if (!wraps->bound)
1167 return 1;
1169 for (i = 1; i < wraps->mat->n_col; ++i)
1170 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1171 return 0;
1173 return 1;
1176 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1177 * to include "set" and add the result in position "w" of "wraps".
1178 * "len" is the total number of coefficients in "bound" and "ineq".
1179 * Return 1 on success, 0 on failure and -1 on error.
1180 * Wrapping can fail if the result of wrapping is equal to "bound"
1181 * or if we want to bound the sizes of the coefficients and
1182 * the wrapped constraint does not satisfy this bound.
1184 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1185 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1187 isl_seq_cpy(wraps->mat->row[w], bound, len);
1188 if (negate) {
1189 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1190 ineq = wraps->mat->row[w + 1];
1192 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1193 return -1;
1194 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1195 return 0;
1196 if (!allow_wrap(wraps, w))
1197 return 0;
1198 return 1;
1201 /* For each constraint in info->bmap that is not redundant (as determined
1202 * by info->tab) and that is not a valid constraint for the other basic map,
1203 * wrap the constraint around "bound" such that it includes the whole
1204 * set "set" and append the resulting constraint to "wraps".
1205 * Note that the constraints that are valid for the other basic map
1206 * will be added to the combined basic map by default, so there is
1207 * no need to wrap them.
1208 * The caller wrap_in_facets even relies on this function not wrapping
1209 * any constraints that are already valid.
1210 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1211 * wraps->n_row is the number of actual wrapped constraints that have
1212 * been added.
1213 * If any of the wrapping problems results in a constraint that is
1214 * identical to "bound", then this means that "set" is unbounded in such
1215 * way that no wrapping is possible. If this happens then wraps->n_row
1216 * is reset to zero.
1217 * Similarly, if we want to bound the coefficients of the wrapping
1218 * constraints and a newly added wrapping constraint does not
1219 * satisfy the bound, then wraps->n_row is also reset to zero.
1221 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1222 isl_int *bound, __isl_keep isl_set *set)
1224 int l, m;
1225 int w;
1226 int added;
1227 isl_basic_map *bmap = info->bmap;
1228 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1230 w = wraps->mat->n_row;
1232 for (l = 0; l < bmap->n_ineq; ++l) {
1233 if (info->ineq[l] == STATUS_VALID ||
1234 info->ineq[l] == STATUS_REDUNDANT)
1235 continue;
1236 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1237 continue;
1238 if (isl_seq_eq(bound, bmap->ineq[l], len))
1239 continue;
1240 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1241 continue;
1243 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1244 if (added < 0)
1245 return -1;
1246 if (!added)
1247 goto unbounded;
1248 ++w;
1250 for (l = 0; l < bmap->n_eq; ++l) {
1251 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1252 continue;
1253 if (isl_seq_eq(bound, bmap->eq[l], len))
1254 continue;
1256 for (m = 0; m < 2; ++m) {
1257 if (info->eq[2 * l + m] == STATUS_VALID)
1258 continue;
1259 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1260 set, !m);
1261 if (added < 0)
1262 return -1;
1263 if (!added)
1264 goto unbounded;
1265 ++w;
1269 wraps->mat->n_row = w;
1270 return 0;
1271 unbounded:
1272 wraps->mat->n_row = 0;
1273 return 0;
1276 /* Check if the constraints in "wraps" from "first" until the last
1277 * are all valid for the basic set represented by "tab".
1278 * If not, wraps->n_row is set to zero.
1280 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1281 struct isl_tab *tab)
1283 int i;
1285 for (i = first; i < wraps->n_row; ++i) {
1286 enum isl_ineq_type type;
1287 type = isl_tab_ineq_type(tab, wraps->row[i]);
1288 if (type == isl_ineq_error)
1289 return -1;
1290 if (type == isl_ineq_redundant)
1291 continue;
1292 wraps->n_row = 0;
1293 return 0;
1296 return 0;
1299 /* Return a set that corresponds to the non-redundant constraints
1300 * (as recorded in tab) of bmap.
1302 * It's important to remove the redundant constraints as some
1303 * of the other constraints may have been modified after the
1304 * constraints were marked redundant.
1305 * In particular, a constraint may have been relaxed.
1306 * Redundant constraints are ignored when a constraint is relaxed
1307 * and should therefore continue to be ignored ever after.
1308 * Otherwise, the relaxation might be thwarted by some of
1309 * these constraints.
1311 * Update the underlying set to ensure that the dimension doesn't change.
1312 * Otherwise the integer divisions could get dropped if the tab
1313 * turns out to be empty.
1315 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1316 struct isl_tab *tab)
1318 isl_basic_set *bset;
1320 bmap = isl_basic_map_copy(bmap);
1321 bset = isl_basic_map_underlying_set(bmap);
1322 bset = isl_basic_set_cow(bset);
1323 bset = isl_basic_set_update_from_tab(bset, tab);
1324 return isl_set_from_basic_set(bset);
1327 /* Wrap the constraints of info->bmap that bound the facet defined
1328 * by inequality "k" around (the opposite of) this inequality to
1329 * include "set". "bound" may be used to store the negated inequality.
1330 * Since the wrapped constraints are not guaranteed to contain the whole
1331 * of info->bmap, we check them in check_wraps.
1332 * If any of the wrapped constraints turn out to be invalid, then
1333 * check_wraps will reset wrap->n_row to zero.
1335 static int add_wraps_around_facet(struct isl_wraps *wraps,
1336 struct isl_coalesce_info *info, int k, isl_int *bound,
1337 __isl_keep isl_set *set)
1339 struct isl_tab_undo *snap;
1340 int n;
1341 unsigned total = isl_basic_map_total_dim(info->bmap);
1343 snap = isl_tab_snap(info->tab);
1345 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1346 return -1;
1347 if (isl_tab_detect_redundant(info->tab) < 0)
1348 return -1;
1350 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1352 n = wraps->mat->n_row;
1353 if (add_wraps(wraps, info, bound, set) < 0)
1354 return -1;
1356 if (isl_tab_rollback(info->tab, snap) < 0)
1357 return -1;
1358 if (check_wraps(wraps->mat, n, info->tab) < 0)
1359 return -1;
1361 return 0;
1364 /* Given a basic set i with a constraint k that is adjacent to
1365 * basic set j, check if we can wrap
1366 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1367 * (always) around their ridges to include the other set.
1368 * If so, replace the pair of basic sets by their union.
1370 * All constraints of i (except k) are assumed to be valid or
1371 * cut constraints for j.
1372 * Wrapping the cut constraints to include basic map j may result
1373 * in constraints that are no longer valid of basic map i
1374 * we have to check that the resulting wrapping constraints are valid for i.
1375 * If "wrap_facet" is not set, then all constraints of i (except k)
1376 * are assumed to be valid for j.
1377 * ____ _____
1378 * / | / \
1379 * / || / |
1380 * \ || => \ |
1381 * \ || \ |
1382 * \___|| \____|
1385 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1386 struct isl_coalesce_info *info, int wrap_facet)
1388 enum isl_change change = isl_change_none;
1389 struct isl_wraps wraps;
1390 isl_ctx *ctx;
1391 isl_mat *mat;
1392 struct isl_set *set_i = NULL;
1393 struct isl_set *set_j = NULL;
1394 struct isl_vec *bound = NULL;
1395 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1397 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1398 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1399 ctx = isl_basic_map_get_ctx(info[i].bmap);
1400 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1401 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1402 1 + total);
1403 wraps_init(&wraps, mat, info, i, j);
1404 bound = isl_vec_alloc(ctx, 1 + total);
1405 if (!set_i || !set_j || !wraps.mat || !bound)
1406 goto error;
1408 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1409 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1411 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1412 wraps.mat->n_row = 1;
1414 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1415 goto error;
1416 if (!wraps.mat->n_row)
1417 goto unbounded;
1419 if (wrap_facet) {
1420 if (add_wraps_around_facet(&wraps, &info[i], k,
1421 bound->el, set_j) < 0)
1422 goto error;
1423 if (!wraps.mat->n_row)
1424 goto unbounded;
1427 change = fuse(i, j, info, wraps.mat, 0, 0);
1429 unbounded:
1430 wraps_free(&wraps);
1432 isl_set_free(set_i);
1433 isl_set_free(set_j);
1435 isl_vec_free(bound);
1437 return change;
1438 error:
1439 wraps_free(&wraps);
1440 isl_vec_free(bound);
1441 isl_set_free(set_i);
1442 isl_set_free(set_j);
1443 return isl_change_error;
1446 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1447 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1448 * add wrapping constraints to wrap.mat for all constraints
1449 * of basic map j that bound the part of basic map j that sticks out
1450 * of the cut constraint.
1451 * "set_i" is the underlying set of basic map i.
1452 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1454 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1455 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1456 * (with respect to the integer points), so we add t(x) >= 0 instead.
1457 * Otherwise, we wrap the constraints of basic map j that are not
1458 * redundant in this intersection and that are not already valid
1459 * for basic map i over basic map i.
1460 * Note that it is sufficient to wrap the constraints to include
1461 * basic map i, because we will only wrap the constraints that do
1462 * not include basic map i already. The wrapped constraint will
1463 * therefore be more relaxed compared to the original constraint.
1464 * Since the original constraint is valid for basic map j, so is
1465 * the wrapped constraint.
1467 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1468 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1469 struct isl_tab_undo *snap)
1471 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1472 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1473 return isl_stat_error;
1474 if (isl_tab_detect_redundant(info_j->tab) < 0)
1475 return isl_stat_error;
1477 if (info_j->tab->empty)
1478 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1479 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1480 return isl_stat_error;
1482 if (isl_tab_rollback(info_j->tab, snap) < 0)
1483 return isl_stat_error;
1485 return isl_stat_ok;
1488 /* Given a pair of basic maps i and j such that j sticks out
1489 * of i at n cut constraints, each time by at most one,
1490 * try to compute wrapping constraints and replace the two
1491 * basic maps by a single basic map.
1492 * The other constraints of i are assumed to be valid for j.
1493 * "set_i" is the underlying set of basic map i.
1494 * "wraps" has been initialized to be of the right size.
1496 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1497 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1498 * of basic map j that bound the part of basic map j that sticks out
1499 * of the cut constraint.
1501 * If any wrapping fails, i.e., if we cannot wrap to touch
1502 * the union, then we give up.
1503 * Otherwise, the pair of basic maps is replaced by their union.
1505 static enum isl_change try_wrap_in_facets(int i, int j,
1506 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1507 __isl_keep isl_set *set_i)
1509 int k, l, w;
1510 unsigned total;
1511 struct isl_tab_undo *snap;
1513 total = isl_basic_map_total_dim(info[i].bmap);
1515 snap = isl_tab_snap(info[j].tab);
1517 wraps->mat->n_row = 0;
1519 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1520 for (l = 0; l < 2; ++l) {
1521 if (info[i].eq[2 * k + l] != STATUS_CUT)
1522 continue;
1523 w = wraps->mat->n_row++;
1524 if (l == 0)
1525 isl_seq_neg(wraps->mat->row[w],
1526 info[i].bmap->eq[k], 1 + total);
1527 else
1528 isl_seq_cpy(wraps->mat->row[w],
1529 info[i].bmap->eq[k], 1 + total);
1530 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1531 return isl_change_error;
1533 if (!wraps->mat->n_row)
1534 return isl_change_none;
1538 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1539 if (info[i].ineq[k] != STATUS_CUT)
1540 continue;
1541 w = wraps->mat->n_row++;
1542 isl_seq_cpy(wraps->mat->row[w],
1543 info[i].bmap->ineq[k], 1 + total);
1544 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1545 return isl_change_error;
1547 if (!wraps->mat->n_row)
1548 return isl_change_none;
1551 return fuse(i, j, info, wraps->mat, 0, 1);
1554 /* Given a pair of basic maps i and j such that j sticks out
1555 * of i at n cut constraints, each time by at most one,
1556 * try to compute wrapping constraints and replace the two
1557 * basic maps by a single basic map.
1558 * The other constraints of i are assumed to be valid for j.
1560 * The core computation is performed by try_wrap_in_facets.
1561 * This function simply extracts an underlying set representation
1562 * of basic map i and initializes the data structure for keeping
1563 * track of wrapping constraints.
1565 static enum isl_change wrap_in_facets(int i, int j, int n,
1566 struct isl_coalesce_info *info)
1568 enum isl_change change = isl_change_none;
1569 struct isl_wraps wraps;
1570 isl_ctx *ctx;
1571 isl_mat *mat;
1572 isl_set *set_i = NULL;
1573 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1574 int max_wrap;
1576 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1577 return isl_change_error;
1579 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1580 max_wrap *= n;
1582 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1583 ctx = isl_basic_map_get_ctx(info[i].bmap);
1584 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1585 wraps_init(&wraps, mat, info, i, j);
1586 if (!set_i || !wraps.mat)
1587 goto error;
1589 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1591 wraps_free(&wraps);
1592 isl_set_free(set_i);
1594 return change;
1595 error:
1596 wraps_free(&wraps);
1597 isl_set_free(set_i);
1598 return isl_change_error;
1601 /* Return the effect of inequality "ineq" on the tableau "tab",
1602 * after relaxing the constant term of "ineq" by one.
1604 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1606 enum isl_ineq_type type;
1608 isl_int_add_ui(ineq[0], ineq[0], 1);
1609 type = isl_tab_ineq_type(tab, ineq);
1610 isl_int_sub_ui(ineq[0], ineq[0], 1);
1612 return type;
1615 /* Given two basic sets i and j,
1616 * check if relaxing all the cut constraints of i by one turns
1617 * them into valid constraint for j and check if we can wrap in
1618 * the bits that are sticking out.
1619 * If so, replace the pair by their union.
1621 * We first check if all relaxed cut inequalities of i are valid for j
1622 * and then try to wrap in the intersections of the relaxed cut inequalities
1623 * with j.
1625 * During this wrapping, we consider the points of j that lie at a distance
1626 * of exactly 1 from i. In particular, we ignore the points that lie in
1627 * between this lower-dimensional space and the basic map i.
1628 * We can therefore only apply this to integer maps.
1629 * ____ _____
1630 * / ___|_ / \
1631 * / | | / |
1632 * \ | | => \ |
1633 * \|____| \ |
1634 * \___| \____/
1636 * _____ ______
1637 * | ____|_ | \
1638 * | | | | |
1639 * | | | => | |
1640 * |_| | | |
1641 * |_____| \______|
1643 * _______
1644 * | |
1645 * | |\ |
1646 * | | \ |
1647 * | | \ |
1648 * | | \|
1649 * | | \
1650 * | |_____\
1651 * | |
1652 * |_______|
1654 * Wrapping can fail if the result of wrapping one of the facets
1655 * around its edges does not produce any new facet constraint.
1656 * In particular, this happens when we try to wrap in unbounded sets.
1658 * _______________________________________________________________________
1660 * | ___
1661 * | | |
1662 * |_| |_________________________________________________________________
1663 * |___|
1665 * The following is not an acceptable result of coalescing the above two
1666 * sets as it includes extra integer points.
1667 * _______________________________________________________________________
1669 * |
1670 * |
1672 * \______________________________________________________________________
1674 static enum isl_change can_wrap_in_set(int i, int j,
1675 struct isl_coalesce_info *info)
1677 int k, l;
1678 int n;
1679 unsigned total;
1681 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1682 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1683 return isl_change_none;
1685 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1686 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1687 if (n == 0)
1688 return isl_change_none;
1690 total = isl_basic_map_total_dim(info[i].bmap);
1691 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1692 for (l = 0; l < 2; ++l) {
1693 enum isl_ineq_type type;
1695 if (info[i].eq[2 * k + l] != STATUS_CUT)
1696 continue;
1698 if (l == 0)
1699 isl_seq_neg(info[i].bmap->eq[k],
1700 info[i].bmap->eq[k], 1 + total);
1701 type = type_of_relaxed(info[j].tab,
1702 info[i].bmap->eq[k]);
1703 if (l == 0)
1704 isl_seq_neg(info[i].bmap->eq[k],
1705 info[i].bmap->eq[k], 1 + total);
1706 if (type == isl_ineq_error)
1707 return isl_change_error;
1708 if (type != isl_ineq_redundant)
1709 return isl_change_none;
1713 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1714 enum isl_ineq_type type;
1716 if (info[i].ineq[k] != STATUS_CUT)
1717 continue;
1719 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1720 if (type == isl_ineq_error)
1721 return isl_change_error;
1722 if (type != isl_ineq_redundant)
1723 return isl_change_none;
1726 return wrap_in_facets(i, j, n, info);
1729 /* Check if either i or j has only cut constraints that can
1730 * be used to wrap in (a facet of) the other basic set.
1731 * if so, replace the pair by their union.
1733 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1735 enum isl_change change = isl_change_none;
1737 change = can_wrap_in_set(i, j, info);
1738 if (change != isl_change_none)
1739 return change;
1741 change = can_wrap_in_set(j, i, info);
1742 return change;
1745 /* Check if all inequality constraints of "i" that cut "j" cease
1746 * to be cut constraints if they are relaxed by one.
1747 * If so, collect the cut constraints in "list".
1748 * The caller is responsible for allocating "list".
1750 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1751 int *list)
1753 int l, n;
1755 n = 0;
1756 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1757 enum isl_ineq_type type;
1759 if (info[i].ineq[l] != STATUS_CUT)
1760 continue;
1761 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1762 if (type == isl_ineq_error)
1763 return isl_bool_error;
1764 if (type != isl_ineq_redundant)
1765 return isl_bool_false;
1766 list[n++] = l;
1769 return isl_bool_true;
1772 /* Given two basic maps such that "j" has at least one equality constraint
1773 * that is adjacent to an inequality constraint of "i" and such that "i" has
1774 * exactly one inequality constraint that is adjacent to an equality
1775 * constraint of "j", check whether "i" can be extended to include "j" or
1776 * whether "j" can be wrapped into "i".
1777 * All remaining constraints of "i" and "j" are assumed to be valid
1778 * or cut constraints of the other basic map.
1779 * However, none of the equality constraints of "i" are cut constraints.
1781 * If "i" has any "cut" inequality constraints, then check if relaxing
1782 * each of them by one is sufficient for them to become valid.
1783 * If so, check if the inequality constraint adjacent to an equality
1784 * constraint of "j" along with all these cut constraints
1785 * can be relaxed by one to contain exactly "j".
1786 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1788 static enum isl_change check_single_adj_eq(int i, int j,
1789 struct isl_coalesce_info *info)
1791 enum isl_change change = isl_change_none;
1792 int k;
1793 int n_cut;
1794 int *relax;
1795 isl_ctx *ctx;
1796 isl_bool try_relax;
1798 n_cut = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1800 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ);
1802 if (n_cut > 0) {
1803 ctx = isl_basic_map_get_ctx(info[i].bmap);
1804 relax = isl_calloc_array(ctx, int, 1 + n_cut);
1805 if (!relax)
1806 return isl_change_error;
1807 relax[0] = k;
1808 try_relax = all_cut_by_one(i, j, info, relax + 1);
1809 if (try_relax < 0)
1810 change = isl_change_error;
1811 } else {
1812 try_relax = isl_bool_true;
1813 relax = &k;
1815 if (try_relax && change == isl_change_none)
1816 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
1817 if (n_cut > 0)
1818 free(relax);
1819 if (change != isl_change_none)
1820 return change;
1822 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
1824 return change;
1827 /* At least one of the basic maps has an equality that is adjacent
1828 * to inequality. Make sure that only one of the basic maps has
1829 * such an equality and that the other basic map has exactly one
1830 * inequality adjacent to an equality.
1831 * If the other basic map does not have such an inequality, then
1832 * check if all its constraints are either valid or cut constraints
1833 * and, if so, try wrapping in the first map into the second.
1834 * Otherwise, try to extend one basic map with the other or
1835 * wrap one basic map in the other.
1837 static enum isl_change check_adj_eq(int i, int j,
1838 struct isl_coalesce_info *info)
1840 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1841 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1842 /* ADJ EQ TOO MANY */
1843 return isl_change_none;
1845 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1846 return check_adj_eq(j, i, info);
1848 /* j has an equality adjacent to an inequality in i */
1850 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1851 if (all_valid_or_cut(&info[i]))
1852 return can_wrap_in_set(i, j, info);
1853 return isl_change_none;
1855 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1856 return isl_change_none;
1857 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1858 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1859 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1860 /* ADJ EQ TOO MANY */
1861 return isl_change_none;
1863 return check_single_adj_eq(i, j, info);
1866 /* The two basic maps lie on adjacent hyperplanes. In particular,
1867 * basic map "i" has an equality that lies parallel to basic map "j".
1868 * Check if we can wrap the facets around the parallel hyperplanes
1869 * to include the other set.
1871 * We perform basically the same operations as can_wrap_in_facet,
1872 * except that we don't need to select a facet of one of the sets.
1874 * \\ \\
1875 * \\ => \\
1876 * \ \|
1878 * If there is more than one equality of "i" adjacent to an equality of "j",
1879 * then the result will satisfy one or more equalities that are a linear
1880 * combination of these equalities. These will be encoded as pairs
1881 * of inequalities in the wrapping constraints and need to be made
1882 * explicit.
1884 static enum isl_change check_eq_adj_eq(int i, int j,
1885 struct isl_coalesce_info *info)
1887 int k;
1888 enum isl_change change = isl_change_none;
1889 int detect_equalities = 0;
1890 struct isl_wraps wraps;
1891 isl_ctx *ctx;
1892 isl_mat *mat;
1893 struct isl_set *set_i = NULL;
1894 struct isl_set *set_j = NULL;
1895 struct isl_vec *bound = NULL;
1896 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1898 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1899 detect_equalities = 1;
1901 k = find(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ);
1903 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1904 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1905 ctx = isl_basic_map_get_ctx(info[i].bmap);
1906 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1907 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1908 1 + total);
1909 wraps_init(&wraps, mat, info, i, j);
1910 bound = isl_vec_alloc(ctx, 1 + total);
1911 if (!set_i || !set_j || !wraps.mat || !bound)
1912 goto error;
1914 if (k % 2 == 0)
1915 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1916 else
1917 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1918 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1920 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1921 wraps.mat->n_row = 1;
1923 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1924 goto error;
1925 if (!wraps.mat->n_row)
1926 goto unbounded;
1928 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1929 isl_seq_neg(bound->el, bound->el, 1 + total);
1931 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1932 wraps.mat->n_row++;
1934 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1935 goto error;
1936 if (!wraps.mat->n_row)
1937 goto unbounded;
1939 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1941 if (0) {
1942 error: change = isl_change_error;
1944 unbounded:
1946 wraps_free(&wraps);
1947 isl_set_free(set_i);
1948 isl_set_free(set_j);
1949 isl_vec_free(bound);
1951 return change;
1954 /* Initialize the "eq" and "ineq" fields of "info".
1956 static void init_status(struct isl_coalesce_info *info)
1958 info->eq = info->ineq = NULL;
1961 /* Set info->eq to the positions of the equalities of info->bmap
1962 * with respect to the basic map represented by "tab".
1963 * If info->eq has already been computed, then do not compute it again.
1965 static void set_eq_status_in(struct isl_coalesce_info *info,
1966 struct isl_tab *tab)
1968 if (info->eq)
1969 return;
1970 info->eq = eq_status_in(info->bmap, tab);
1973 /* Set info->ineq to the positions of the inequalities of info->bmap
1974 * with respect to the basic map represented by "tab".
1975 * If info->ineq has already been computed, then do not compute it again.
1977 static void set_ineq_status_in(struct isl_coalesce_info *info,
1978 struct isl_tab *tab)
1980 if (info->ineq)
1981 return;
1982 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1985 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1986 * This function assumes that init_status has been called on "info" first,
1987 * after which the "eq" and "ineq" fields may or may not have been
1988 * assigned a newly allocated array.
1990 static void clear_status(struct isl_coalesce_info *info)
1992 free(info->eq);
1993 free(info->ineq);
1996 /* Are all inequality constraints of the basic map represented by "info"
1997 * valid for the other basic map, except for a single constraint
1998 * that is adjacent to an inequality constraint of the other basic map?
2000 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2002 int i;
2003 int k = -1;
2005 for (i = 0; i < info->bmap->n_ineq; ++i) {
2006 if (info->ineq[i] == STATUS_REDUNDANT)
2007 continue;
2008 if (info->ineq[i] == STATUS_VALID)
2009 continue;
2010 if (info->ineq[i] != STATUS_ADJ_INEQ)
2011 return 0;
2012 if (k != -1)
2013 return 0;
2014 k = i;
2017 return k != -1;
2020 /* Basic map "i" has one or more equality constraints that separate it
2021 * from basic map "j". Check if it happens to be an extension
2022 * of basic map "j".
2023 * In particular, check that all constraints of "j" are valid for "i",
2024 * except for one inequality constraint that is adjacent
2025 * to an inequality constraints of "i".
2026 * If so, check for "i" being an extension of "j" by calling
2027 * is_adj_ineq_extension.
2029 * Clean up the memory allocated for keeping track of the status
2030 * of the constraints before returning.
2032 static enum isl_change separating_equality(int i, int j,
2033 struct isl_coalesce_info *info)
2035 enum isl_change change = isl_change_none;
2037 if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2038 all_ineq_valid_or_single_adj_ineq(&info[j]))
2039 change = is_adj_ineq_extension(j, i, info);
2041 clear_status(&info[i]);
2042 clear_status(&info[j]);
2043 return change;
2046 /* Check if the union of the given pair of basic maps
2047 * can be represented by a single basic map.
2048 * If so, replace the pair by the single basic map and return
2049 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2050 * Otherwise, return isl_change_none.
2051 * The two basic maps are assumed to live in the same local space.
2052 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2053 * to have been initialized by the caller, either to NULL or
2054 * to valid information.
2056 * We first check the effect of each constraint of one basic map
2057 * on the other basic map.
2058 * The constraint may be
2059 * redundant the constraint is redundant in its own
2060 * basic map and should be ignore and removed
2061 * in the end
2062 * valid all (integer) points of the other basic map
2063 * satisfy the constraint
2064 * separate no (integer) point of the other basic map
2065 * satisfies the constraint
2066 * cut some but not all points of the other basic map
2067 * satisfy the constraint
2068 * adj_eq the given constraint is adjacent (on the outside)
2069 * to an equality of the other basic map
2070 * adj_ineq the given constraint is adjacent (on the outside)
2071 * to an inequality of the other basic map
2073 * We consider seven cases in which we can replace the pair by a single
2074 * basic map. We ignore all "redundant" constraints.
2076 * 1. all constraints of one basic map are valid
2077 * => the other basic map is a subset and can be removed
2079 * 2. all constraints of both basic maps are either "valid" or "cut"
2080 * and the facets corresponding to the "cut" constraints
2081 * of one of the basic maps lies entirely inside the other basic map
2082 * => the pair can be replaced by a basic map consisting
2083 * of the valid constraints in both basic maps
2085 * 3. there is a single pair of adjacent inequalities
2086 * (all other constraints are "valid")
2087 * => the pair can be replaced by a basic map consisting
2088 * of the valid constraints in both basic maps
2090 * 4. one basic map has a single adjacent inequality, while the other
2091 * constraints are "valid". The other basic map has some
2092 * "cut" constraints, but replacing the adjacent inequality by
2093 * its opposite and adding the valid constraints of the other
2094 * basic map results in a subset of the other basic map
2095 * => the pair can be replaced by a basic map consisting
2096 * of the valid constraints in both basic maps
2098 * 5. there is a single adjacent pair of an inequality and an equality,
2099 * the other constraints of the basic map containing the inequality are
2100 * "valid". Moreover, if the inequality the basic map is relaxed
2101 * and then turned into an equality, then resulting facet lies
2102 * entirely inside the other basic map
2103 * => the pair can be replaced by the basic map containing
2104 * the inequality, with the inequality relaxed.
2106 * 6. there is a single adjacent pair of an inequality and an equality,
2107 * the other constraints of the basic map containing the inequality are
2108 * "valid". Moreover, the facets corresponding to both
2109 * the inequality and the equality can be wrapped around their
2110 * ridges to include the other basic map
2111 * => the pair can be replaced by a basic map consisting
2112 * of the valid constraints in both basic maps together
2113 * with all wrapping constraints
2115 * 7. one of the basic maps extends beyond the other by at most one.
2116 * Moreover, the facets corresponding to the cut constraints and
2117 * the pieces of the other basic map at offset one from these cut
2118 * constraints can be wrapped around their ridges to include
2119 * the union of the two basic maps
2120 * => the pair can be replaced by a basic map consisting
2121 * of the valid constraints in both basic maps together
2122 * with all wrapping constraints
2124 * 8. the two basic maps live in adjacent hyperplanes. In principle
2125 * such sets can always be combined through wrapping, but we impose
2126 * that there is only one such pair, to avoid overeager coalescing.
2128 * Throughout the computation, we maintain a collection of tableaus
2129 * corresponding to the basic maps. When the basic maps are dropped
2130 * or combined, the tableaus are modified accordingly.
2132 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2133 struct isl_coalesce_info *info)
2135 enum isl_change change = isl_change_none;
2137 set_ineq_status_in(&info[i], info[j].tab);
2138 if (info[i].bmap->n_ineq && !info[i].ineq)
2139 goto error;
2140 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
2141 goto error;
2142 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
2143 goto done;
2145 set_ineq_status_in(&info[j], info[i].tab);
2146 if (info[j].bmap->n_ineq && !info[j].ineq)
2147 goto error;
2148 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2149 goto error;
2150 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2151 goto done;
2153 set_eq_status_in(&info[i], info[j].tab);
2154 if (info[i].bmap->n_eq && !info[i].eq)
2155 goto error;
2156 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
2157 goto error;
2159 set_eq_status_in(&info[j], info[i].tab);
2160 if (info[j].bmap->n_eq && !info[j].eq)
2161 goto error;
2162 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
2163 goto error;
2165 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
2166 return separating_equality(i, j, info);
2167 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
2168 return separating_equality(j, i, info);
2170 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2171 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2172 drop(&info[j]);
2173 change = isl_change_drop_second;
2174 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2175 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2176 drop(&info[i]);
2177 change = isl_change_drop_first;
2178 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2179 change = check_eq_adj_eq(i, j, info);
2180 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2181 change = check_eq_adj_eq(j, i, info);
2182 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2183 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2184 change = check_adj_eq(i, j, info);
2185 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2186 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2187 /* Can't happen */
2188 /* BAD ADJ INEQ */
2189 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2190 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2191 change = check_adj_ineq(i, j, info);
2192 } else {
2193 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2194 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2195 change = check_facets(i, j, info);
2196 if (change == isl_change_none)
2197 change = check_wrap(i, j, info);
2200 done:
2201 clear_status(&info[i]);
2202 clear_status(&info[j]);
2203 return change;
2204 error:
2205 clear_status(&info[i]);
2206 clear_status(&info[j]);
2207 return isl_change_error;
2210 /* Check if the union of the given pair of basic maps
2211 * can be represented by a single basic map.
2212 * If so, replace the pair by the single basic map and return
2213 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2214 * Otherwise, return isl_change_none.
2215 * The two basic maps are assumed to live in the same local space.
2217 static enum isl_change coalesce_local_pair(int i, int j,
2218 struct isl_coalesce_info *info)
2220 init_status(&info[i]);
2221 init_status(&info[j]);
2222 return coalesce_local_pair_reuse(i, j, info);
2225 /* Shift the integer division at position "div" of the basic map
2226 * represented by "info" by "shift".
2228 * That is, if the integer division has the form
2230 * floor(f(x)/d)
2232 * then replace it by
2234 * floor((f(x) + shift * d)/d) - shift
2236 static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2237 isl_int shift)
2239 unsigned total;
2241 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2242 if (!info->bmap)
2243 return isl_stat_error;
2245 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2246 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2247 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2248 return isl_stat_error;
2250 return isl_stat_ok;
2253 /* If the integer division at position "div" is defined by an equality,
2254 * i.e., a stride constraint, then change the integer division expression
2255 * to have a constant term equal to zero.
2257 * Let the equality constraint be
2259 * c + f + m a = 0
2261 * The integer division expression is then of the form
2263 * a = floor((-f - c')/m)
2265 * The integer division is first shifted by t = floor(c/m),
2266 * turning the equality constraint into
2268 * c - m floor(c/m) + f + m a' = 0
2270 * i.e.,
2272 * (c mod m) + f + m a' = 0
2274 * That is,
2276 * a' = (-f - (c mod m))/m = floor((-f)/m)
2278 * because a' is an integer and 0 <= (c mod m) < m.
2279 * The constant term of a' can therefore be zeroed out.
2281 static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2283 isl_bool defined;
2284 isl_stat r;
2285 isl_constraint *c;
2286 isl_int shift, stride;
2288 defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2289 div, &c);
2290 if (defined < 0)
2291 return isl_stat_error;
2292 if (!defined)
2293 return isl_stat_ok;
2294 if (!c)
2295 return isl_stat_error;
2296 isl_int_init(shift);
2297 isl_int_init(stride);
2298 isl_constraint_get_constant(c, &shift);
2299 isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2300 isl_int_fdiv_q(shift, shift, stride);
2301 r = shift_div(info, div, shift);
2302 isl_int_clear(stride);
2303 isl_int_clear(shift);
2304 isl_constraint_free(c);
2305 if (r < 0)
2306 return isl_stat_error;
2307 info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2308 info->bmap, div, 0);
2309 if (!info->bmap)
2310 return isl_stat_error;
2311 return isl_stat_ok;
2314 /* The basic maps represented by "info1" and "info2" are known
2315 * to have the same number of integer divisions.
2316 * Check if pairs of integer divisions are equal to each other
2317 * despite the fact that they differ by a rational constant.
2319 * In particular, look for any pair of integer divisions that
2320 * only differ in their constant terms.
2321 * If either of these integer divisions is defined
2322 * by stride constraints, then modify it to have a zero constant term.
2323 * If both are defined by stride constraints then in the end they will have
2324 * the same (zero) constant term.
2326 static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2327 struct isl_coalesce_info *info2)
2329 int i, n;
2330 int total;
2332 total = isl_basic_map_total_dim(info1->bmap);
2333 n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2334 for (i = 0; i < n; ++i) {
2335 isl_bool known, harmonize;
2337 known = isl_basic_map_div_is_known(info1->bmap, i);
2338 if (known >= 0 && known)
2339 known = isl_basic_map_div_is_known(info2->bmap, i);
2340 if (known < 0)
2341 return isl_stat_error;
2342 if (!known)
2343 continue;
2344 harmonize = isl_basic_map_equal_div_expr_except_constant(
2345 info1->bmap, i, info2->bmap, i);
2346 if (harmonize < 0)
2347 return isl_stat_error;
2348 if (!harmonize)
2349 continue;
2350 if (normalize_stride_div(info1, i) < 0)
2351 return isl_stat_error;
2352 if (normalize_stride_div(info2, i) < 0)
2353 return isl_stat_error;
2356 return isl_stat_ok;
2359 /* If "shift" is an integer constant, then shift the integer division
2360 * at position "div" of the basic map represented by "info" by "shift".
2361 * If "shift" is not an integer constant, then do nothing.
2362 * If "shift" is equal to zero, then no shift needs to be performed either.
2364 * That is, if the integer division has the form
2366 * floor(f(x)/d)
2368 * then replace it by
2370 * floor((f(x) + shift * d)/d) - shift
2372 static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2373 __isl_keep isl_aff *shift)
2375 isl_bool cst;
2376 isl_stat r;
2377 isl_int d;
2378 isl_val *c;
2380 cst = isl_aff_is_cst(shift);
2381 if (cst < 0 || !cst)
2382 return cst < 0 ? isl_stat_error : isl_stat_ok;
2384 c = isl_aff_get_constant_val(shift);
2385 cst = isl_val_is_int(c);
2386 if (cst >= 0 && cst)
2387 cst = isl_bool_not(isl_val_is_zero(c));
2388 if (cst < 0 || !cst) {
2389 isl_val_free(c);
2390 return cst < 0 ? isl_stat_error : isl_stat_ok;
2393 isl_int_init(d);
2394 r = isl_val_get_num_isl_int(c, &d);
2395 if (r >= 0)
2396 r = shift_div(info, div, d);
2397 isl_int_clear(d);
2399 isl_val_free(c);
2401 return r;
2404 /* Check if some of the divs in the basic map represented by "info1"
2405 * are shifts of the corresponding divs in the basic map represented
2406 * by "info2", taking into account the equality constraints "eq1" of "info1"
2407 * and "eq2" of "info2". If so, align them with those of "info2".
2408 * "info1" and "info2" are assumed to have the same number
2409 * of integer divisions.
2411 * An integer division is considered to be a shift of another integer
2412 * division if, after simplification with respect to the equality
2413 * constraints of the other basic map, one is equal to the other
2414 * plus a constant.
2416 * In particular, for each pair of integer divisions, if both are known,
2417 * have the same denominator and are not already equal to each other,
2418 * simplify each with respect to the equality constraints
2419 * of the other basic map. If the difference is an integer constant,
2420 * then move this difference outside.
2421 * That is, if, after simplification, one integer division is of the form
2423 * floor((f(x) + c_1)/d)
2425 * while the other is of the form
2427 * floor((f(x) + c_2)/d)
2429 * and n = (c_2 - c_1)/d is an integer, then replace the first
2430 * integer division by
2432 * floor((f_1(x) + c_1 + n * d)/d) - n,
2434 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2435 * after simplification with respect to the equality constraints.
2437 static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2438 struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2439 __isl_keep isl_basic_set *eq2)
2441 int i;
2442 int total;
2443 isl_local_space *ls1, *ls2;
2445 total = isl_basic_map_total_dim(info1->bmap);
2446 ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2447 ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2448 for (i = 0; i < info1->bmap->n_div; ++i) {
2449 isl_stat r;
2450 isl_aff *div1, *div2;
2452 if (!isl_local_space_div_is_known(ls1, i) ||
2453 !isl_local_space_div_is_known(ls2, i))
2454 continue;
2455 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2456 continue;
2457 if (isl_seq_eq(info1->bmap->div[i] + 1,
2458 info2->bmap->div[i] + 1, 1 + total))
2459 continue;
2460 div1 = isl_local_space_get_div(ls1, i);
2461 div2 = isl_local_space_get_div(ls2, i);
2462 div1 = isl_aff_substitute_equalities(div1,
2463 isl_basic_set_copy(eq2));
2464 div2 = isl_aff_substitute_equalities(div2,
2465 isl_basic_set_copy(eq1));
2466 div2 = isl_aff_sub(div2, div1);
2467 r = shift_if_cst_int(info1, i, div2);
2468 isl_aff_free(div2);
2469 if (r < 0)
2470 break;
2472 isl_local_space_free(ls1);
2473 isl_local_space_free(ls2);
2475 if (i < info1->bmap->n_div)
2476 return isl_stat_error;
2477 return isl_stat_ok;
2480 /* Check if some of the divs in the basic map represented by "info1"
2481 * are shifts of the corresponding divs in the basic map represented
2482 * by "info2". If so, align them with those of "info2".
2483 * Only do this if "info1" and "info2" have the same number
2484 * of integer divisions.
2486 * An integer division is considered to be a shift of another integer
2487 * division if, after simplification with respect to the equality
2488 * constraints of the other basic map, one is equal to the other
2489 * plus a constant.
2491 * First check if pairs of integer divisions are equal to each other
2492 * despite the fact that they differ by a rational constant.
2493 * If so, try and arrange for them to have the same constant term.
2495 * Then, extract the equality constraints and continue with
2496 * harmonize_divs_with_hulls.
2498 * If the equality constraints of both basic maps are the same,
2499 * then there is no need to perform any shifting since
2500 * the coefficients of the integer divisions should have been
2501 * reduced in the same way.
2503 static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2504 struct isl_coalesce_info *info2)
2506 isl_bool equal;
2507 isl_basic_map *bmap1, *bmap2;
2508 isl_basic_set *eq1, *eq2;
2509 isl_stat r;
2511 if (!info1->bmap || !info2->bmap)
2512 return isl_stat_error;
2514 if (info1->bmap->n_div != info2->bmap->n_div)
2515 return isl_stat_ok;
2516 if (info1->bmap->n_div == 0)
2517 return isl_stat_ok;
2519 if (harmonize_stride_divs(info1, info2) < 0)
2520 return isl_stat_error;
2522 bmap1 = isl_basic_map_copy(info1->bmap);
2523 bmap2 = isl_basic_map_copy(info2->bmap);
2524 eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2525 eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2526 equal = isl_basic_set_plain_is_equal(eq1, eq2);
2527 if (equal < 0)
2528 r = isl_stat_error;
2529 else if (equal)
2530 r = isl_stat_ok;
2531 else
2532 r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2533 isl_basic_set_free(eq1);
2534 isl_basic_set_free(eq2);
2536 return r;
2539 /* Do the two basic maps live in the same local space, i.e.,
2540 * do they have the same (known) divs?
2541 * If either basic map has any unknown divs, then we can only assume
2542 * that they do not live in the same local space.
2544 static int same_divs(__isl_keep isl_basic_map *bmap1,
2545 __isl_keep isl_basic_map *bmap2)
2547 int i;
2548 int known;
2549 int total;
2551 if (!bmap1 || !bmap2)
2552 return -1;
2553 if (bmap1->n_div != bmap2->n_div)
2554 return 0;
2556 if (bmap1->n_div == 0)
2557 return 1;
2559 known = isl_basic_map_divs_known(bmap1);
2560 if (known < 0 || !known)
2561 return known;
2562 known = isl_basic_map_divs_known(bmap2);
2563 if (known < 0 || !known)
2564 return known;
2566 total = isl_basic_map_total_dim(bmap1);
2567 for (i = 0; i < bmap1->n_div; ++i)
2568 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2569 return 0;
2571 return 1;
2574 /* Assuming that "tab" contains the equality constraints and
2575 * the initial inequality constraints of "bmap", copy the remaining
2576 * inequality constraints of "bmap" to "Tab".
2578 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2580 int i, n_ineq;
2582 if (!bmap)
2583 return isl_stat_error;
2585 n_ineq = tab->n_con - tab->n_eq;
2586 for (i = n_ineq; i < bmap->n_ineq; ++i)
2587 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2588 return isl_stat_error;
2590 return isl_stat_ok;
2593 /* Description of an integer division that is added
2594 * during an expansion.
2595 * "pos" is the position of the corresponding variable.
2596 * "cst" indicates whether this integer division has a fixed value.
2597 * "val" contains the fixed value, if the value is fixed.
2599 struct isl_expanded {
2600 int pos;
2601 isl_bool cst;
2602 isl_int val;
2605 /* For each of the "n" integer division variables "expanded",
2606 * if the variable has a fixed value, then add two inequality
2607 * constraints expressing the fixed value.
2608 * Otherwise, add the corresponding div constraints.
2609 * The caller is responsible for removing the div constraints
2610 * that it added for all these "n" integer divisions.
2612 * The div constraints and the pair of inequality constraints
2613 * forcing the fixed value cannot both be added for a given variable
2614 * as the combination may render some of the original constraints redundant.
2615 * These would then be ignored during the coalescing detection,
2616 * while they could remain in the fused result.
2618 * The two added inequality constraints are
2620 * -a + v >= 0
2621 * a - v >= 0
2623 * with "a" the variable and "v" its fixed value.
2624 * The facet corresponding to one of these two constraints is selected
2625 * in the tableau to ensure that the pair of inequality constraints
2626 * is treated as an equality constraint.
2628 * The information in info->ineq is thrown away because it was
2629 * computed in terms of div constraints, while some of those
2630 * have now been replaced by these pairs of inequality constraints.
2632 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2633 int n, struct isl_expanded *expanded)
2635 unsigned o_div;
2636 int i;
2637 isl_vec *ineq;
2639 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2640 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2641 if (!ineq)
2642 return isl_stat_error;
2643 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2645 for (i = 0; i < n; ++i) {
2646 if (!expanded[i].cst) {
2647 info->bmap = isl_basic_map_extend_constraints(
2648 info->bmap, 0, 2);
2649 if (isl_basic_map_add_div_constraints(info->bmap,
2650 expanded[i].pos - o_div) < 0)
2651 break;
2652 } else {
2653 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2654 isl_int_set(ineq->el[0], expanded[i].val);
2655 info->bmap = isl_basic_map_add_ineq(info->bmap,
2656 ineq->el);
2657 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2658 isl_int_neg(ineq->el[0], expanded[i].val);
2659 info->bmap = isl_basic_map_add_ineq(info->bmap,
2660 ineq->el);
2661 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2663 if (copy_ineq(info->tab, info->bmap) < 0)
2664 break;
2665 if (expanded[i].cst &&
2666 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2667 break;
2670 isl_vec_free(ineq);
2672 clear_status(info);
2673 init_status(info);
2675 return i < n ? isl_stat_error : isl_stat_ok;
2678 /* Insert the "n" integer division variables "expanded"
2679 * into info->tab and info->bmap and
2680 * update info->ineq with respect to the redundant constraints
2681 * in the resulting tableau.
2682 * "bmap" contains the result of this insertion in info->bmap,
2683 * while info->bmap is the original version
2684 * of "bmap", i.e., the one that corresponds to the current
2685 * state of info->tab. The number of constraints in info->bmap
2686 * is assumed to be the same as the number of constraints
2687 * in info->tab. This is required to be able to detect
2688 * the extra constraints in "bmap".
2690 * In particular, introduce extra variables corresponding
2691 * to the extra integer divisions and add the div constraints
2692 * that were added to "bmap" after info->tab was created
2693 * from info->bmap.
2694 * Furthermore, check if these extra integer divisions happen
2695 * to attain a fixed integer value in info->tab.
2696 * If so, replace the corresponding div constraints by pairs
2697 * of inequality constraints that fix these
2698 * integer divisions to their single integer values.
2699 * Replace info->bmap by "bmap" to match the changes to info->tab.
2700 * info->ineq was computed without a tableau and therefore
2701 * does not take into account the redundant constraints
2702 * in the tableau. Mark them here.
2703 * There is no need to check the newly added div constraints
2704 * since they cannot be redundant.
2705 * The redundancy check is not performed when constants have been discovered
2706 * since info->ineq is completely thrown away in this case.
2708 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2709 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2711 int i, n_ineq;
2712 unsigned n_eq;
2713 struct isl_tab_undo *snap;
2714 int any;
2716 if (!bmap)
2717 return isl_stat_error;
2718 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2719 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2720 "original tableau does not correspond "
2721 "to original basic map", goto error);
2723 if (isl_tab_extend_vars(info->tab, n) < 0)
2724 goto error;
2725 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2726 goto error;
2728 for (i = 0; i < n; ++i) {
2729 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2730 goto error;
2733 snap = isl_tab_snap(info->tab);
2735 n_ineq = info->tab->n_con - info->tab->n_eq;
2736 if (copy_ineq(info->tab, bmap) < 0)
2737 goto error;
2739 isl_basic_map_free(info->bmap);
2740 info->bmap = bmap;
2742 any = 0;
2743 for (i = 0; i < n; ++i) {
2744 expanded[i].cst = isl_tab_is_constant(info->tab,
2745 expanded[i].pos, &expanded[i].val);
2746 if (expanded[i].cst < 0)
2747 return isl_stat_error;
2748 if (expanded[i].cst)
2749 any = 1;
2752 if (any) {
2753 if (isl_tab_rollback(info->tab, snap) < 0)
2754 return isl_stat_error;
2755 info->bmap = isl_basic_map_cow(info->bmap);
2756 if (isl_basic_map_free_inequality(info->bmap, 2 * n) < 0)
2757 return isl_stat_error;
2759 return fix_constant_divs(info, n, expanded);
2762 n_eq = info->bmap->n_eq;
2763 for (i = 0; i < n_ineq; ++i) {
2764 if (isl_tab_is_redundant(info->tab, n_eq + i))
2765 info->ineq[i] = STATUS_REDUNDANT;
2768 return isl_stat_ok;
2769 error:
2770 isl_basic_map_free(bmap);
2771 return isl_stat_error;
2774 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2775 * in isl_basic_map_expand_divs using the expansion "exp" and
2776 * update info->ineq with respect to the redundant constraints
2777 * in the resulting tableau. info->bmap is the original version
2778 * of "bmap", i.e., the one that corresponds to the current
2779 * state of info->tab. The number of constraints in info->bmap
2780 * is assumed to be the same as the number of constraints
2781 * in info->tab. This is required to be able to detect
2782 * the extra constraints in "bmap".
2784 * Extract the positions where extra local variables are introduced
2785 * from "exp" and call tab_insert_divs.
2787 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2788 __isl_take isl_basic_map *bmap)
2790 isl_ctx *ctx;
2791 struct isl_expanded *expanded;
2792 int i, j, k, n;
2793 int extra_var;
2794 unsigned total, pos, n_div;
2795 isl_stat r;
2797 total = isl_basic_map_dim(bmap, isl_dim_all);
2798 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2799 pos = total - n_div;
2800 extra_var = total - info->tab->n_var;
2801 n = n_div - extra_var;
2803 ctx = isl_basic_map_get_ctx(bmap);
2804 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
2805 if (extra_var && !expanded)
2806 goto error;
2808 i = 0;
2809 k = 0;
2810 for (j = 0; j < n_div; ++j) {
2811 if (i < n && exp[i] == j) {
2812 ++i;
2813 continue;
2815 expanded[k++].pos = pos + j;
2818 for (k = 0; k < extra_var; ++k)
2819 isl_int_init(expanded[k].val);
2821 r = tab_insert_divs(info, extra_var, expanded, bmap);
2823 for (k = 0; k < extra_var; ++k)
2824 isl_int_clear(expanded[k].val);
2825 free(expanded);
2827 return r;
2828 error:
2829 isl_basic_map_free(bmap);
2830 return isl_stat_error;
2833 /* Check if the union of the basic maps represented by info[i] and info[j]
2834 * can be represented by a single basic map,
2835 * after expanding the divs of info[i] to match those of info[j].
2836 * If so, replace the pair by the single basic map and return
2837 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2838 * Otherwise, return isl_change_none.
2840 * The caller has already checked for info[j] being a subset of info[i].
2841 * If some of the divs of info[j] are unknown, then the expanded info[i]
2842 * will not have the corresponding div constraints. The other patterns
2843 * therefore cannot apply. Skip the computation in this case.
2845 * The expansion is performed using the divs "div" and expansion "exp"
2846 * computed by the caller.
2847 * info[i].bmap has already been expanded and the result is passed in
2848 * as "bmap".
2849 * The "eq" and "ineq" fields of info[i] reflect the status of
2850 * the constraints of the expanded "bmap" with respect to info[j].tab.
2851 * However, inequality constraints that are redundant in info[i].tab
2852 * have not yet been marked as such because no tableau was available.
2854 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2855 * updating info[i].ineq with respect to the redundant constraints.
2856 * Then try and coalesce the expanded info[i] with info[j],
2857 * reusing the information in info[i].eq and info[i].ineq.
2858 * If this does not result in any coalescing or if it results in info[j]
2859 * getting dropped (which should not happen in practice, since the case
2860 * of info[j] being a subset of info[i] has already been checked by
2861 * the caller), then revert info[i] to its original state.
2863 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2864 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2865 int *exp)
2867 isl_bool known;
2868 isl_basic_map *bmap_i;
2869 struct isl_tab_undo *snap;
2870 enum isl_change change = isl_change_none;
2872 known = isl_basic_map_divs_known(info[j].bmap);
2873 if (known < 0 || !known) {
2874 clear_status(&info[i]);
2875 isl_basic_map_free(bmap);
2876 return known < 0 ? isl_change_error : isl_change_none;
2879 bmap_i = isl_basic_map_copy(info[i].bmap);
2880 snap = isl_tab_snap(info[i].tab);
2881 if (expand_tab(&info[i], exp, bmap) < 0)
2882 change = isl_change_error;
2884 init_status(&info[j]);
2885 if (change == isl_change_none)
2886 change = coalesce_local_pair_reuse(i, j, info);
2887 else
2888 clear_status(&info[i]);
2889 if (change != isl_change_none && change != isl_change_drop_second) {
2890 isl_basic_map_free(bmap_i);
2891 } else {
2892 isl_basic_map_free(info[i].bmap);
2893 info[i].bmap = bmap_i;
2895 if (isl_tab_rollback(info[i].tab, snap) < 0)
2896 change = isl_change_error;
2899 return change;
2902 /* Check if the union of "bmap" and the basic map represented by info[j]
2903 * can be represented by a single basic map,
2904 * after expanding the divs of "bmap" to match those of info[j].
2905 * If so, replace the pair by the single basic map and return
2906 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2907 * Otherwise, return isl_change_none.
2909 * In particular, check if the expanded "bmap" contains the basic map
2910 * represented by the tableau info[j].tab.
2911 * The expansion is performed using the divs "div" and expansion "exp"
2912 * computed by the caller.
2913 * Then we check if all constraints of the expanded "bmap" are valid for
2914 * info[j].tab.
2916 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2917 * In this case, the positions of the constraints of info[i].bmap
2918 * with respect to the basic map represented by info[j] are stored
2919 * in info[i].
2921 * If the expanded "bmap" does not contain the basic map
2922 * represented by the tableau info[j].tab and if "i" is not -1,
2923 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2924 * as well and check if that results in coalescing.
2926 static enum isl_change coalesce_with_expanded_divs(
2927 __isl_keep isl_basic_map *bmap, int i, int j,
2928 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2930 enum isl_change change = isl_change_none;
2931 struct isl_coalesce_info info_local, *info_i;
2933 info_i = i >= 0 ? &info[i] : &info_local;
2934 init_status(info_i);
2935 bmap = isl_basic_map_copy(bmap);
2936 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2937 bmap = isl_basic_map_mark_final(bmap);
2939 if (!bmap)
2940 goto error;
2942 info_i->eq = eq_status_in(bmap, info[j].tab);
2943 if (bmap->n_eq && !info_i->eq)
2944 goto error;
2945 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2946 goto error;
2947 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2948 goto done;
2950 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2951 if (bmap->n_ineq && !info_i->ineq)
2952 goto error;
2953 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2954 goto error;
2955 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2956 goto done;
2958 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2959 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2960 drop(&info[j]);
2961 change = isl_change_drop_second;
2964 if (change == isl_change_none && i != -1)
2965 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2967 done:
2968 isl_basic_map_free(bmap);
2969 clear_status(info_i);
2970 return change;
2971 error:
2972 isl_basic_map_free(bmap);
2973 clear_status(info_i);
2974 return isl_change_error;
2977 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2978 * can be represented by a single basic map,
2979 * after aligning the divs of "bmap_i" to match those of info[j].
2980 * If so, replace the pair by the single basic map and return
2981 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2982 * Otherwise, return isl_change_none.
2984 * In particular, check if "bmap_i" contains the basic map represented by
2985 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2986 * Note that this can only succeed if the number of divs of "bmap_i"
2987 * is smaller than (or equal to) the number of divs of info[j].
2989 * We first check if the divs of "bmap_i" are all known and form a subset
2990 * of those of info[j].bmap. If so, we pass control over to
2991 * coalesce_with_expanded_divs.
2993 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2995 static enum isl_change coalesce_after_aligning_divs(
2996 __isl_keep isl_basic_map *bmap_i, int i, int j,
2997 struct isl_coalesce_info *info)
2999 int known;
3000 isl_mat *div_i, *div_j, *div;
3001 int *exp1 = NULL;
3002 int *exp2 = NULL;
3003 isl_ctx *ctx;
3004 enum isl_change change;
3006 known = isl_basic_map_divs_known(bmap_i);
3007 if (known < 0 || !known)
3008 return known;
3010 ctx = isl_basic_map_get_ctx(bmap_i);
3012 div_i = isl_basic_map_get_divs(bmap_i);
3013 div_j = isl_basic_map_get_divs(info[j].bmap);
3015 if (!div_i || !div_j)
3016 goto error;
3018 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3019 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3020 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
3021 goto error;
3023 div = isl_merge_divs(div_i, div_j, exp1, exp2);
3024 if (!div)
3025 goto error;
3027 if (div->n_row == div_j->n_row)
3028 change = coalesce_with_expanded_divs(bmap_i,
3029 i, j, info, div, exp1);
3030 else
3031 change = isl_change_none;
3033 isl_mat_free(div);
3035 isl_mat_free(div_i);
3036 isl_mat_free(div_j);
3038 free(exp2);
3039 free(exp1);
3041 return change;
3042 error:
3043 isl_mat_free(div_i);
3044 isl_mat_free(div_j);
3045 free(exp1);
3046 free(exp2);
3047 return isl_change_error;
3050 /* Check if basic map "j" is a subset of basic map "i" after
3051 * exploiting the extra equalities of "j" to simplify the divs of "i".
3052 * If so, remove basic map "j" and return isl_change_drop_second.
3054 * If "j" does not have any equalities or if they are the same
3055 * as those of "i", then we cannot exploit them to simplify the divs.
3056 * Similarly, if there are no divs in "i", then they cannot be simplified.
3057 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3058 * then "j" cannot be a subset of "i".
3060 * Otherwise, we intersect "i" with the affine hull of "j" and then
3061 * check if "j" is a subset of the result after aligning the divs.
3062 * If so, then "j" is definitely a subset of "i" and can be removed.
3063 * Note that if after intersection with the affine hull of "j".
3064 * "i" still has more divs than "j", then there is no way we can
3065 * align the divs of "i" to those of "j".
3067 static enum isl_change coalesce_subset_with_equalities(int i, int j,
3068 struct isl_coalesce_info *info)
3070 isl_basic_map *hull_i, *hull_j, *bmap_i;
3071 int equal, empty;
3072 enum isl_change change;
3074 if (info[j].bmap->n_eq == 0)
3075 return isl_change_none;
3076 if (info[i].bmap->n_div == 0)
3077 return isl_change_none;
3079 hull_i = isl_basic_map_copy(info[i].bmap);
3080 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3081 hull_j = isl_basic_map_copy(info[j].bmap);
3082 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3084 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3085 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3086 empty = isl_basic_map_plain_is_empty(hull_j);
3087 isl_basic_map_free(hull_i);
3089 if (equal < 0 || equal || empty < 0 || empty) {
3090 isl_basic_map_free(hull_j);
3091 if (equal < 0 || empty < 0)
3092 return isl_change_error;
3093 return isl_change_none;
3096 bmap_i = isl_basic_map_copy(info[i].bmap);
3097 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3098 if (!bmap_i)
3099 return isl_change_error;
3101 if (bmap_i->n_div > info[j].bmap->n_div) {
3102 isl_basic_map_free(bmap_i);
3103 return isl_change_none;
3106 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3108 isl_basic_map_free(bmap_i);
3110 return change;
3113 /* Check if the union of and the basic maps represented by info[i] and info[j]
3114 * can be represented by a single basic map, by aligning or equating
3115 * their integer divisions.
3116 * If so, replace the pair by the single basic map and return
3117 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3118 * Otherwise, return isl_change_none.
3120 * Note that we only perform any test if the number of divs is different
3121 * in the two basic maps. In case the number of divs is the same,
3122 * we have already established that the divs are different
3123 * in the two basic maps.
3124 * In particular, if the number of divs of basic map i is smaller than
3125 * the number of divs of basic map j, then we check if j is a subset of i
3126 * and vice versa.
3128 static enum isl_change coalesce_divs(int i, int j,
3129 struct isl_coalesce_info *info)
3131 enum isl_change change = isl_change_none;
3133 if (info[i].bmap->n_div < info[j].bmap->n_div)
3134 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3135 if (change != isl_change_none)
3136 return change;
3138 if (info[j].bmap->n_div < info[i].bmap->n_div)
3139 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3140 if (change != isl_change_none)
3141 return invert_change(change);
3143 change = coalesce_subset_with_equalities(i, j, info);
3144 if (change != isl_change_none)
3145 return change;
3147 change = coalesce_subset_with_equalities(j, i, info);
3148 if (change != isl_change_none)
3149 return invert_change(change);
3151 return isl_change_none;
3154 /* Does "bmap" involve any divs that themselves refer to divs?
3156 static int has_nested_div(__isl_keep isl_basic_map *bmap)
3158 int i;
3159 unsigned total;
3160 unsigned n_div;
3162 total = isl_basic_map_dim(bmap, isl_dim_all);
3163 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3164 total -= n_div;
3166 for (i = 0; i < n_div; ++i)
3167 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
3168 n_div) != -1)
3169 return 1;
3171 return 0;
3174 /* Return a list of affine expressions, one for each integer division
3175 * in "bmap_i". For each integer division that also appears in "bmap_j",
3176 * the affine expression is set to NaN. The number of NaNs in the list
3177 * is equal to the number of integer divisions in "bmap_j".
3178 * For the other integer divisions of "bmap_i", the corresponding
3179 * element in the list is a purely affine expression equal to the integer
3180 * division in "hull".
3181 * If no such list can be constructed, then the number of elements
3182 * in the returned list is smaller than the number of integer divisions
3183 * in "bmap_i".
3185 static __isl_give isl_aff_list *set_up_substitutions(
3186 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3187 __isl_take isl_basic_map *hull)
3189 unsigned n_div_i, n_div_j, total;
3190 isl_ctx *ctx;
3191 isl_local_space *ls;
3192 isl_basic_set *wrap_hull;
3193 isl_aff *aff_nan;
3194 isl_aff_list *list;
3195 int i, j;
3197 if (!hull)
3198 return NULL;
3200 ctx = isl_basic_map_get_ctx(hull);
3202 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3203 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3204 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
3206 ls = isl_basic_map_get_local_space(bmap_i);
3207 ls = isl_local_space_wrap(ls);
3208 wrap_hull = isl_basic_map_wrap(hull);
3210 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3211 list = isl_aff_list_alloc(ctx, n_div_i);
3213 j = 0;
3214 for (i = 0; i < n_div_i; ++i) {
3215 isl_aff *aff;
3217 if (j < n_div_j &&
3218 isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3219 0, 2 + total)) {
3220 ++j;
3221 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3222 continue;
3224 if (n_div_i - i <= n_div_j - j)
3225 break;
3227 aff = isl_local_space_get_div(ls, i);
3228 aff = isl_aff_substitute_equalities(aff,
3229 isl_basic_set_copy(wrap_hull));
3230 aff = isl_aff_floor(aff);
3231 if (!aff)
3232 goto error;
3233 if (isl_aff_dim(aff, isl_dim_div) != 0) {
3234 isl_aff_free(aff);
3235 break;
3238 list = isl_aff_list_add(list, aff);
3241 isl_aff_free(aff_nan);
3242 isl_local_space_free(ls);
3243 isl_basic_set_free(wrap_hull);
3245 return list;
3246 error:
3247 isl_aff_free(aff_nan);
3248 isl_local_space_free(ls);
3249 isl_basic_set_free(wrap_hull);
3250 isl_aff_list_free(list);
3251 return NULL;
3254 /* Add variables to info->bmap and info->tab corresponding to the elements
3255 * in "list" that are not set to NaN.
3256 * "extra_var" is the number of these elements.
3257 * "dim" is the offset in the variables of "tab" where we should
3258 * start considering the elements in "list".
3259 * When this function returns, the total number of variables in "tab"
3260 * is equal to "dim" plus the number of elements in "list".
3262 * The newly added existentially quantified variables are not given
3263 * an explicit representation because the corresponding div constraints
3264 * do not appear in info->bmap. These constraints are not added
3265 * to info->bmap because for internal consistency, they would need to
3266 * be added to info->tab as well, where they could combine with the equality
3267 * that is added later to result in constraints that do not hold
3268 * in the original input.
3270 static int add_sub_vars(struct isl_coalesce_info *info,
3271 __isl_keep isl_aff_list *list, int dim, int extra_var)
3273 int i, j, n, d;
3274 isl_space *space;
3276 space = isl_basic_map_get_space(info->bmap);
3277 info->bmap = isl_basic_map_cow(info->bmap);
3278 info->bmap = isl_basic_map_extend_space(info->bmap, space,
3279 extra_var, 0, 0);
3280 if (!info->bmap)
3281 return -1;
3282 n = isl_aff_list_n_aff(list);
3283 for (i = 0; i < n; ++i) {
3284 int is_nan;
3285 isl_aff *aff;
3287 aff = isl_aff_list_get_aff(list, i);
3288 is_nan = isl_aff_is_nan(aff);
3289 isl_aff_free(aff);
3290 if (is_nan < 0)
3291 return -1;
3292 if (is_nan)
3293 continue;
3295 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3296 return -1;
3297 d = isl_basic_map_alloc_div(info->bmap);
3298 if (d < 0)
3299 return -1;
3300 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3301 if (!info->bmap)
3302 return -1;
3303 for (j = d; j > i; --j)
3304 isl_basic_map_swap_div(info->bmap, j - 1, j);
3307 return 0;
3310 /* For each element in "list" that is not set to NaN, fix the corresponding
3311 * variable in "tab" to the purely affine expression defined by the element.
3312 * "dim" is the offset in the variables of "tab" where we should
3313 * start considering the elements in "list".
3315 * This function assumes that a sufficient number of rows and
3316 * elements in the constraint array are available in the tableau.
3318 static int add_sub_equalities(struct isl_tab *tab,
3319 __isl_keep isl_aff_list *list, int dim)
3321 int i, n;
3322 isl_ctx *ctx;
3323 isl_vec *sub;
3324 isl_aff *aff;
3326 n = isl_aff_list_n_aff(list);
3328 ctx = isl_tab_get_ctx(tab);
3329 sub = isl_vec_alloc(ctx, 1 + dim + n);
3330 if (!sub)
3331 return -1;
3332 isl_seq_clr(sub->el + 1 + dim, n);
3334 for (i = 0; i < n; ++i) {
3335 aff = isl_aff_list_get_aff(list, i);
3336 if (!aff)
3337 goto error;
3338 if (isl_aff_is_nan(aff)) {
3339 isl_aff_free(aff);
3340 continue;
3342 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3343 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3344 if (isl_tab_add_eq(tab, sub->el) < 0)
3345 goto error;
3346 isl_int_set_si(sub->el[1 + dim + i], 0);
3347 isl_aff_free(aff);
3350 isl_vec_free(sub);
3351 return 0;
3352 error:
3353 isl_aff_free(aff);
3354 isl_vec_free(sub);
3355 return -1;
3358 /* Add variables to info->tab and info->bmap corresponding to the elements
3359 * in "list" that are not set to NaN. The value of the added variable
3360 * in info->tab is fixed to the purely affine expression defined by the element.
3361 * "dim" is the offset in the variables of info->tab where we should
3362 * start considering the elements in "list".
3363 * When this function returns, the total number of variables in info->tab
3364 * is equal to "dim" plus the number of elements in "list".
3366 static int add_subs(struct isl_coalesce_info *info,
3367 __isl_keep isl_aff_list *list, int dim)
3369 int extra_var;
3370 int n;
3372 if (!list)
3373 return -1;
3375 n = isl_aff_list_n_aff(list);
3376 extra_var = n - (info->tab->n_var - dim);
3378 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3379 return -1;
3380 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3381 return -1;
3382 if (add_sub_vars(info, list, dim, extra_var) < 0)
3383 return -1;
3385 return add_sub_equalities(info->tab, list, dim);
3388 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3389 * divisions in "i" but not in "j" to basic map "j", with values
3390 * specified by "list". The total number of elements in "list"
3391 * is equal to the number of integer divisions in "i", while the number
3392 * of NaN elements in the list is equal to the number of integer divisions
3393 * in "j".
3395 * If no coalescing can be performed, then we need to revert basic map "j"
3396 * to its original state. We do the same if basic map "i" gets dropped
3397 * during the coalescing, even though this should not happen in practice
3398 * since we have already checked for "j" being a subset of "i"
3399 * before we reach this stage.
3401 static enum isl_change coalesce_with_subs(int i, int j,
3402 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3404 isl_basic_map *bmap_j;
3405 struct isl_tab_undo *snap;
3406 unsigned dim;
3407 enum isl_change change;
3409 bmap_j = isl_basic_map_copy(info[j].bmap);
3410 snap = isl_tab_snap(info[j].tab);
3412 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3413 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
3414 if (add_subs(&info[j], list, dim) < 0)
3415 goto error;
3417 change = coalesce_local_pair(i, j, info);
3418 if (change != isl_change_none && change != isl_change_drop_first) {
3419 isl_basic_map_free(bmap_j);
3420 } else {
3421 isl_basic_map_free(info[j].bmap);
3422 info[j].bmap = bmap_j;
3424 if (isl_tab_rollback(info[j].tab, snap) < 0)
3425 return isl_change_error;
3428 return change;
3429 error:
3430 isl_basic_map_free(bmap_j);
3431 return isl_change_error;
3434 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3435 * those extra integer divisions in "i" that can be simplified away
3436 * using the extra equalities in "j".
3437 * All divs are assumed to be known and not contain any nested divs.
3439 * We first check if there are any extra equalities in "j" that we
3440 * can exploit. Then we check if every integer division in "i"
3441 * either already appears in "j" or can be simplified using the
3442 * extra equalities to a purely affine expression.
3443 * If these tests succeed, then we try to coalesce the two basic maps
3444 * by introducing extra dimensions in "j" corresponding to
3445 * the extra integer divsisions "i" fixed to the corresponding
3446 * purely affine expression.
3448 static enum isl_change check_coalesce_into_eq(int i, int j,
3449 struct isl_coalesce_info *info)
3451 unsigned n_div_i, n_div_j;
3452 isl_basic_map *hull_i, *hull_j;
3453 int equal, empty;
3454 isl_aff_list *list;
3455 enum isl_change change;
3457 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3458 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3459 if (n_div_i <= n_div_j)
3460 return isl_change_none;
3461 if (info[j].bmap->n_eq == 0)
3462 return isl_change_none;
3464 hull_i = isl_basic_map_copy(info[i].bmap);
3465 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3466 hull_j = isl_basic_map_copy(info[j].bmap);
3467 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3469 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3470 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3471 empty = isl_basic_map_plain_is_empty(hull_j);
3472 isl_basic_map_free(hull_i);
3474 if (equal < 0 || empty < 0)
3475 goto error;
3476 if (equal || empty) {
3477 isl_basic_map_free(hull_j);
3478 return isl_change_none;
3481 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3482 if (!list)
3483 return isl_change_error;
3484 if (isl_aff_list_n_aff(list) < n_div_i)
3485 change = isl_change_none;
3486 else
3487 change = coalesce_with_subs(i, j, info, list);
3489 isl_aff_list_free(list);
3491 return change;
3492 error:
3493 isl_basic_map_free(hull_j);
3494 return isl_change_error;
3497 /* Check if we can coalesce basic maps "i" and "j" after copying
3498 * those extra integer divisions in one of the basic maps that can
3499 * be simplified away using the extra equalities in the other basic map.
3500 * We require all divs to be known in both basic maps.
3501 * Furthermore, to simplify the comparison of div expressions,
3502 * we do not allow any nested integer divisions.
3504 static enum isl_change check_coalesce_eq(int i, int j,
3505 struct isl_coalesce_info *info)
3507 int known, nested;
3508 enum isl_change change;
3510 known = isl_basic_map_divs_known(info[i].bmap);
3511 if (known < 0 || !known)
3512 return known < 0 ? isl_change_error : isl_change_none;
3513 known = isl_basic_map_divs_known(info[j].bmap);
3514 if (known < 0 || !known)
3515 return known < 0 ? isl_change_error : isl_change_none;
3516 nested = has_nested_div(info[i].bmap);
3517 if (nested < 0 || nested)
3518 return nested < 0 ? isl_change_error : isl_change_none;
3519 nested = has_nested_div(info[j].bmap);
3520 if (nested < 0 || nested)
3521 return nested < 0 ? isl_change_error : isl_change_none;
3523 change = check_coalesce_into_eq(i, j, info);
3524 if (change != isl_change_none)
3525 return change;
3526 change = check_coalesce_into_eq(j, i, info);
3527 if (change != isl_change_none)
3528 return invert_change(change);
3530 return isl_change_none;
3533 /* Check if the union of the given pair of basic maps
3534 * can be represented by a single basic map.
3535 * If so, replace the pair by the single basic map and return
3536 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3537 * Otherwise, return isl_change_none.
3539 * We first check if the two basic maps live in the same local space,
3540 * after aligning the divs that differ by only an integer constant.
3541 * If so, we do the complete check. Otherwise, we check if they have
3542 * the same number of integer divisions and can be coalesced, if one is
3543 * an obvious subset of the other or if the extra integer divisions
3544 * of one basic map can be simplified away using the extra equalities
3545 * of the other basic map.
3547 static enum isl_change coalesce_pair(int i, int j,
3548 struct isl_coalesce_info *info)
3550 int same;
3551 enum isl_change change;
3553 if (harmonize_divs(&info[i], &info[j]) < 0)
3554 return isl_change_error;
3555 same = same_divs(info[i].bmap, info[j].bmap);
3556 if (same < 0)
3557 return isl_change_error;
3558 if (same)
3559 return coalesce_local_pair(i, j, info);
3561 if (info[i].bmap->n_div == info[j].bmap->n_div) {
3562 change = coalesce_local_pair(i, j, info);
3563 if (change != isl_change_none)
3564 return change;
3567 change = coalesce_divs(i, j, info);
3568 if (change != isl_change_none)
3569 return change;
3571 return check_coalesce_eq(i, j, info);
3574 /* Return the maximum of "a" and "b".
3576 static int isl_max(int a, int b)
3578 return a > b ? a : b;
3581 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3582 * with those in the range [start2, end2[, skipping basic maps
3583 * that have been removed (either before or within this function).
3585 * For each basic map i in the first range, we check if it can be coalesced
3586 * with respect to any previously considered basic map j in the second range.
3587 * If i gets dropped (because it was a subset of some j), then
3588 * we can move on to the next basic map.
3589 * If j gets dropped, we need to continue checking against the other
3590 * previously considered basic maps.
3591 * If the two basic maps got fused, then we recheck the fused basic map
3592 * against the previously considered basic maps, starting at i + 1
3593 * (even if start2 is greater than i + 1).
3595 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3596 int start1, int end1, int start2, int end2)
3598 int i, j;
3600 for (i = end1 - 1; i >= start1; --i) {
3601 if (info[i].removed)
3602 continue;
3603 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3604 enum isl_change changed;
3606 if (info[j].removed)
3607 continue;
3608 if (info[i].removed)
3609 isl_die(ctx, isl_error_internal,
3610 "basic map unexpectedly removed",
3611 return -1);
3612 changed = coalesce_pair(i, j, info);
3613 switch (changed) {
3614 case isl_change_error:
3615 return -1;
3616 case isl_change_none:
3617 case isl_change_drop_second:
3618 continue;
3619 case isl_change_drop_first:
3620 j = end2;
3621 break;
3622 case isl_change_fuse:
3623 j = i;
3624 break;
3629 return 0;
3632 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3634 * We consider groups of basic maps that live in the same apparent
3635 * affine hull and we first coalesce within such a group before we
3636 * coalesce the elements in the group with elements of previously
3637 * considered groups. If a fuse happens during the second phase,
3638 * then we also reconsider the elements within the group.
3640 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3642 int start, end;
3644 for (end = n; end > 0; end = start) {
3645 start = end - 1;
3646 while (start >= 1 &&
3647 info[start - 1].hull_hash == info[start].hull_hash)
3648 start--;
3649 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3650 return -1;
3651 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3652 return -1;
3655 return 0;
3658 /* Update the basic maps in "map" based on the information in "info".
3659 * In particular, remove the basic maps that have been marked removed and
3660 * update the others based on the information in the corresponding tableau.
3661 * Since we detected implicit equalities without calling
3662 * isl_basic_map_gauss, we need to do it now.
3663 * Also call isl_basic_map_simplify if we may have lost the definition
3664 * of one or more integer divisions.
3666 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3667 int n, struct isl_coalesce_info *info)
3669 int i;
3671 if (!map)
3672 return NULL;
3674 for (i = n - 1; i >= 0; --i) {
3675 if (info[i].removed) {
3676 isl_basic_map_free(map->p[i]);
3677 if (i != map->n - 1)
3678 map->p[i] = map->p[map->n - 1];
3679 map->n--;
3680 continue;
3683 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3684 info[i].tab);
3685 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3686 if (info[i].simplify)
3687 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3688 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3689 if (!info[i].bmap)
3690 return isl_map_free(map);
3691 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3692 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3693 isl_basic_map_free(map->p[i]);
3694 map->p[i] = info[i].bmap;
3695 info[i].bmap = NULL;
3698 return map;
3701 /* For each pair of basic maps in the map, check if the union of the two
3702 * can be represented by a single basic map.
3703 * If so, replace the pair by the single basic map and start over.
3705 * We factor out any (hidden) common factor from the constraint
3706 * coefficients to improve the detection of adjacent constraints.
3708 * Since we are constructing the tableaus of the basic maps anyway,
3709 * we exploit them to detect implicit equalities and redundant constraints.
3710 * This also helps the coalescing as it can ignore the redundant constraints.
3711 * In order to avoid confusion, we make all implicit equalities explicit
3712 * in the basic maps. We don't call isl_basic_map_gauss, though,
3713 * as that may affect the number of constraints.
3714 * This means that we have to call isl_basic_map_gauss at the end
3715 * of the computation (in update_basic_maps) to ensure that
3716 * the basic maps are not left in an unexpected state.
3717 * For each basic map, we also compute the hash of the apparent affine hull
3718 * for use in coalesce.
3720 struct isl_map *isl_map_coalesce(struct isl_map *map)
3722 int i;
3723 unsigned n;
3724 isl_ctx *ctx;
3725 struct isl_coalesce_info *info = NULL;
3727 map = isl_map_remove_empty_parts(map);
3728 if (!map)
3729 return NULL;
3731 if (map->n <= 1)
3732 return map;
3734 ctx = isl_map_get_ctx(map);
3735 map = isl_map_sort_divs(map);
3736 map = isl_map_cow(map);
3738 if (!map)
3739 return NULL;
3741 n = map->n;
3743 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3744 if (!info)
3745 goto error;
3747 for (i = 0; i < map->n; ++i) {
3748 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3749 if (!map->p[i])
3750 goto error;
3751 info[i].bmap = isl_basic_map_copy(map->p[i]);
3752 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3753 if (!info[i].tab)
3754 goto error;
3755 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3756 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3757 goto error;
3758 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3759 info[i].bmap);
3760 if (!info[i].bmap)
3761 goto error;
3762 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3763 if (isl_tab_detect_redundant(info[i].tab) < 0)
3764 goto error;
3765 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3766 goto error;
3768 for (i = map->n - 1; i >= 0; --i)
3769 if (info[i].tab->empty)
3770 drop(&info[i]);
3772 if (coalesce(ctx, n, info) < 0)
3773 goto error;
3775 map = update_basic_maps(map, n, info);
3777 clear_coalesce_info(n, info);
3779 return map;
3780 error:
3781 clear_coalesce_info(n, info);
3782 isl_map_free(map);
3783 return NULL;
3786 /* For each pair of basic sets in the set, check if the union of the two
3787 * can be represented by a single basic set.
3788 * If so, replace the pair by the single basic set and start over.
3790 struct isl_set *isl_set_coalesce(struct isl_set *set)
3792 return set_from_map(isl_map_coalesce(set_to_map(set)));