isl_local_space.c: normalize_div: return modified result
[isl.git] / isl_coalesce.c
blob836d6ca91776b5a18c7432324b2ac5106f258a19
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 inequalities 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 /* Is there any (half of an) equality constraint in the description
200 * of the basic map represented by "info" that
201 * has position "status" with respect to the other basic map?
203 static int any_eq(struct isl_coalesce_info *info, int status)
205 unsigned n_eq;
207 n_eq = isl_basic_map_n_equality(info->bmap);
208 return any(info->eq, 2 * n_eq, status);
211 /* Is there any inequality constraint in the description
212 * of the basic map represented by "info" that
213 * has position "status" with respect to the other basic map?
215 static int any_ineq(struct isl_coalesce_info *info, int status)
217 unsigned n_ineq;
219 n_ineq = isl_basic_map_n_inequality(info->bmap);
220 return any(info->ineq, n_ineq, status);
223 /* Return the position of the first half on an equality constraint
224 * in the description of the basic map represented by "info" that
225 * has position "status" with respect to the other basic map.
226 * The returned value is twice the position of the equality constraint
227 * plus zero for the negative half and plus one for the positive half.
228 * Return -1 if there is no such entry.
230 static int find_eq(struct isl_coalesce_info *info, int status)
232 unsigned n_eq;
234 n_eq = isl_basic_map_n_equality(info->bmap);
235 return find(info->eq, 2 * n_eq, status);
238 /* Return the position of the first inequality constraint in the description
239 * of the basic map represented by "info" that
240 * has position "status" with respect to the other basic map.
241 * Return -1 if there is no such entry.
243 static int find_ineq(struct isl_coalesce_info *info, int status)
245 unsigned n_ineq;
247 n_ineq = isl_basic_map_n_inequality(info->bmap);
248 return find(info->ineq, n_ineq, status);
251 /* Return the number of (halves of) equality constraints in the description
252 * of the basic map represented by "info" that
253 * have position "status" with respect to the other basic map.
255 static int count_eq(struct isl_coalesce_info *info, int status)
257 unsigned n_eq;
259 n_eq = isl_basic_map_n_equality(info->bmap);
260 return count(info->eq, 2 * n_eq, status);
263 /* Return the number of inequality constraints in the description
264 * of the basic map represented by "info" that
265 * have position "status" with respect to the other basic map.
267 static int count_ineq(struct isl_coalesce_info *info, int status)
269 unsigned n_ineq;
271 n_ineq = isl_basic_map_n_inequality(info->bmap);
272 return count(info->ineq, n_ineq, status);
275 /* Are all non-redundant constraints of the basic map represented by "info"
276 * either valid or cut constraints with respect to the other basic map?
278 static int all_valid_or_cut(struct isl_coalesce_info *info)
280 int i;
282 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
283 if (info->eq[i] == STATUS_REDUNDANT)
284 continue;
285 if (info->eq[i] == STATUS_VALID)
286 continue;
287 if (info->eq[i] == STATUS_CUT)
288 continue;
289 return 0;
292 for (i = 0; i < info->bmap->n_ineq; ++i) {
293 if (info->ineq[i] == STATUS_REDUNDANT)
294 continue;
295 if (info->ineq[i] == STATUS_VALID)
296 continue;
297 if (info->ineq[i] == STATUS_CUT)
298 continue;
299 return 0;
302 return 1;
305 /* Compute the hash of the (apparent) affine hull of info->bmap (with
306 * the existentially quantified variables removed) and store it
307 * in info->hash.
309 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
311 isl_basic_map *hull;
312 unsigned n_div;
314 hull = isl_basic_map_copy(info->bmap);
315 hull = isl_basic_map_plain_affine_hull(hull);
316 n_div = isl_basic_map_dim(hull, isl_dim_div);
317 hull = isl_basic_map_drop_constraints_involving_dims(hull,
318 isl_dim_div, 0, n_div);
319 info->hull_hash = isl_basic_map_get_hash(hull);
320 isl_basic_map_free(hull);
322 return hull ? 0 : -1;
325 /* Free all the allocated memory in an array
326 * of "n" isl_coalesce_info elements.
328 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
330 int i;
332 if (!info)
333 return;
335 for (i = 0; i < n; ++i) {
336 isl_basic_map_free(info[i].bmap);
337 isl_tab_free(info[i].tab);
340 free(info);
343 /* Drop the basic map represented by "info".
344 * That is, clear the memory associated to the entry and
345 * mark it as having been removed.
346 * Gaussian elimination needs to be performed on the basic map
347 * before it gets freed because it may have been put
348 * in an inconsistent state in isl_map_coalesce while it may
349 * be shared with other maps.
351 static void drop(struct isl_coalesce_info *info)
353 info->bmap = isl_basic_map_gauss(info->bmap, NULL);
354 info->bmap = isl_basic_map_free(info->bmap);
355 isl_tab_free(info->tab);
356 info->tab = NULL;
357 info->removed = 1;
360 /* Exchange the information in "info1" with that in "info2".
362 static void exchange(struct isl_coalesce_info *info1,
363 struct isl_coalesce_info *info2)
365 struct isl_coalesce_info info;
367 info = *info1;
368 *info1 = *info2;
369 *info2 = info;
372 /* This type represents the kind of change that has been performed
373 * while trying to coalesce two basic maps.
375 * isl_change_none: nothing was changed
376 * isl_change_drop_first: the first basic map was removed
377 * isl_change_drop_second: the second basic map was removed
378 * isl_change_fuse: the two basic maps were replaced by a new basic map.
380 enum isl_change {
381 isl_change_error = -1,
382 isl_change_none = 0,
383 isl_change_drop_first,
384 isl_change_drop_second,
385 isl_change_fuse,
388 /* Update "change" based on an interchange of the first and the second
389 * basic map. That is, interchange isl_change_drop_first and
390 * isl_change_drop_second.
392 static enum isl_change invert_change(enum isl_change change)
394 switch (change) {
395 case isl_change_error:
396 return isl_change_error;
397 case isl_change_none:
398 return isl_change_none;
399 case isl_change_drop_first:
400 return isl_change_drop_second;
401 case isl_change_drop_second:
402 return isl_change_drop_first;
403 case isl_change_fuse:
404 return isl_change_fuse;
407 return isl_change_error;
410 /* Add the valid constraints of the basic map represented by "info"
411 * to "bmap". "len" is the size of the constraints.
412 * If only one of the pair of inequalities that make up an equality
413 * is valid, then add that inequality.
415 static __isl_give isl_basic_map *add_valid_constraints(
416 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
417 unsigned len)
419 int k, l;
421 if (!bmap)
422 return NULL;
424 for (k = 0; k < info->bmap->n_eq; ++k) {
425 if (info->eq[2 * k] == STATUS_VALID &&
426 info->eq[2 * k + 1] == STATUS_VALID) {
427 l = isl_basic_map_alloc_equality(bmap);
428 if (l < 0)
429 return isl_basic_map_free(bmap);
430 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
431 } else if (info->eq[2 * k] == STATUS_VALID) {
432 l = isl_basic_map_alloc_inequality(bmap);
433 if (l < 0)
434 return isl_basic_map_free(bmap);
435 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
436 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
437 l = isl_basic_map_alloc_inequality(bmap);
438 if (l < 0)
439 return isl_basic_map_free(bmap);
440 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
444 for (k = 0; k < info->bmap->n_ineq; ++k) {
445 if (info->ineq[k] != STATUS_VALID)
446 continue;
447 l = isl_basic_map_alloc_inequality(bmap);
448 if (l < 0)
449 return isl_basic_map_free(bmap);
450 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
453 return bmap;
456 /* Is "bmap" defined by a number of (non-redundant) constraints that
457 * is greater than the number of constraints of basic maps i and j combined?
458 * Equalities are counted as two inequalities.
460 static int number_of_constraints_increases(int i, int j,
461 struct isl_coalesce_info *info,
462 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
464 int k, n_old, n_new;
466 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
467 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
469 n_new = 2 * bmap->n_eq;
470 for (k = 0; k < bmap->n_ineq; ++k)
471 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
472 ++n_new;
474 return n_new > n_old;
477 /* Replace the pair of basic maps i and j by the basic map bounded
478 * by the valid constraints in both basic maps and the constraints
479 * in extra (if not NULL).
480 * Place the fused basic map in the position that is the smallest of i and j.
482 * If "detect_equalities" is set, then look for equalities encoded
483 * as pairs of inequalities.
484 * If "check_number" is set, then the original basic maps are only
485 * replaced if the total number of constraints does not increase.
486 * While the number of integer divisions in the two basic maps
487 * is assumed to be the same, the actual definitions may be different.
488 * We only copy the definition from one of the basic map if it is
489 * the same as that of the other basic map. Otherwise, we mark
490 * the integer division as unknown and simplify the basic map
491 * in an attempt to recover the integer division definition.
493 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
494 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
496 int k, l;
497 struct isl_basic_map *fused = NULL;
498 struct isl_tab *fused_tab = NULL;
499 unsigned total = isl_basic_map_total_dim(info[i].bmap);
500 unsigned extra_rows = extra ? extra->n_row : 0;
501 unsigned n_eq, n_ineq;
502 int simplify = 0;
504 if (j < i)
505 return fuse(j, i, info, extra, detect_equalities, check_number);
507 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
508 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
509 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
510 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
511 fused = add_valid_constraints(fused, &info[i], 1 + total);
512 fused = add_valid_constraints(fused, &info[j], 1 + total);
513 if (!fused)
514 goto error;
515 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
516 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
517 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
519 for (k = 0; k < info[i].bmap->n_div; ++k) {
520 int l = isl_basic_map_alloc_div(fused);
521 if (l < 0)
522 goto error;
523 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
524 1 + 1 + total)) {
525 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
526 1 + 1 + total);
527 } else {
528 isl_int_set_si(fused->div[l][0], 0);
529 simplify = 1;
533 for (k = 0; k < extra_rows; ++k) {
534 l = isl_basic_map_alloc_inequality(fused);
535 if (l < 0)
536 goto error;
537 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
540 if (detect_equalities)
541 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
542 fused = isl_basic_map_gauss(fused, NULL);
543 if (simplify || info[j].simplify) {
544 fused = isl_basic_map_simplify(fused);
545 info[i].simplify = 0;
547 fused = isl_basic_map_finalize(fused);
549 fused_tab = isl_tab_from_basic_map(fused, 0);
550 if (isl_tab_detect_redundant(fused_tab) < 0)
551 goto error;
553 if (check_number &&
554 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
555 isl_tab_free(fused_tab);
556 isl_basic_map_free(fused);
557 return isl_change_none;
560 isl_basic_map_free(info[i].bmap);
561 info[i].bmap = fused;
562 isl_tab_free(info[i].tab);
563 info[i].tab = fused_tab;
564 drop(&info[j]);
566 return isl_change_fuse;
567 error:
568 isl_tab_free(fused_tab);
569 isl_basic_map_free(fused);
570 return isl_change_error;
573 /* Given a pair of basic maps i and j such that all constraints are either
574 * "valid" or "cut", check if the facets corresponding to the "cut"
575 * constraints of i lie entirely within basic map j.
576 * If so, replace the pair by the basic map consisting of the valid
577 * constraints in both basic maps.
578 * Checking whether the facet lies entirely within basic map j
579 * is performed by checking whether the constraints of basic map j
580 * are valid for the facet. These tests are performed on a rational
581 * tableau to avoid the theoretical possibility that a constraint
582 * that was considered to be a cut constraint for the entire basic map i
583 * happens to be considered to be a valid constraint for the facet,
584 * even though it cuts off the same rational points.
586 * To see that we are not introducing any extra points, call the
587 * two basic maps A and B and the resulting map U and let x
588 * be an element of U \setminus ( A \cup B ).
589 * A line connecting x with an element of A \cup B meets a facet F
590 * of either A or B. Assume it is a facet of B and let c_1 be
591 * the corresponding facet constraint. We have c_1(x) < 0 and
592 * so c_1 is a cut constraint. This implies that there is some
593 * (possibly rational) point x' satisfying the constraints of A
594 * and the opposite of c_1 as otherwise c_1 would have been marked
595 * valid for A. The line connecting x and x' meets a facet of A
596 * in a (possibly rational) point that also violates c_1, but this
597 * is impossible since all cut constraints of B are valid for all
598 * cut facets of A.
599 * In case F is a facet of A rather than B, then we can apply the
600 * above reasoning to find a facet of B separating x from A \cup B first.
602 static enum isl_change check_facets(int i, int j,
603 struct isl_coalesce_info *info)
605 int k, l;
606 struct isl_tab_undo *snap, *snap2;
607 unsigned n_eq = info[i].bmap->n_eq;
609 snap = isl_tab_snap(info[i].tab);
610 if (isl_tab_mark_rational(info[i].tab) < 0)
611 return isl_change_error;
612 snap2 = isl_tab_snap(info[i].tab);
614 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
615 if (info[i].ineq[k] != STATUS_CUT)
616 continue;
617 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
618 return isl_change_error;
619 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
620 int stat;
621 if (info[j].ineq[l] != STATUS_CUT)
622 continue;
623 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
624 if (stat < 0)
625 return isl_change_error;
626 if (stat != STATUS_VALID)
627 break;
629 if (isl_tab_rollback(info[i].tab, snap2) < 0)
630 return isl_change_error;
631 if (l < info[j].bmap->n_ineq)
632 break;
635 if (k < info[i].bmap->n_ineq) {
636 if (isl_tab_rollback(info[i].tab, snap) < 0)
637 return isl_change_error;
638 return isl_change_none;
640 return fuse(i, j, info, NULL, 0, 0);
643 /* Check if info->bmap contains the basic map represented
644 * by the tableau "tab".
645 * For each equality, we check both the constraint itself
646 * (as an inequality) and its negation. Make sure the
647 * equality is returned to its original state before returning.
649 static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
651 int k;
652 unsigned dim;
653 isl_basic_map *bmap = info->bmap;
655 dim = isl_basic_map_total_dim(bmap);
656 for (k = 0; k < bmap->n_eq; ++k) {
657 int stat;
658 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
659 stat = status_in(bmap->eq[k], tab);
660 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
661 if (stat < 0)
662 return isl_bool_error;
663 if (stat != STATUS_VALID)
664 return isl_bool_false;
665 stat = status_in(bmap->eq[k], tab);
666 if (stat < 0)
667 return isl_bool_error;
668 if (stat != STATUS_VALID)
669 return isl_bool_false;
672 for (k = 0; k < bmap->n_ineq; ++k) {
673 int stat;
674 if (info->ineq[k] == STATUS_REDUNDANT)
675 continue;
676 stat = status_in(bmap->ineq[k], tab);
677 if (stat < 0)
678 return isl_bool_error;
679 if (stat != STATUS_VALID)
680 return isl_bool_false;
682 return isl_bool_true;
685 /* Basic map "i" has an inequality (say "k") that is adjacent
686 * to some inequality of basic map "j". All the other inequalities
687 * are valid for "j".
688 * Check if basic map "j" forms an extension of basic map "i".
690 * Note that this function is only called if some of the equalities or
691 * inequalities of basic map "j" do cut basic map "i". The function is
692 * correct even if there are no such cut constraints, but in that case
693 * the additional checks performed by this function are overkill.
695 * In particular, we replace constraint k, say f >= 0, by constraint
696 * f <= -1, add the inequalities of "j" that are valid for "i"
697 * and check if the result is a subset of basic map "j".
698 * To improve the chances of the subset relation being detected,
699 * any variable that only attains a single integer value
700 * in the tableau of "i" is first fixed to that value.
701 * If the result is a subset, then we know that this result is exactly equal
702 * to basic map "j" since all its constraints are valid for basic map "j".
703 * By combining the valid constraints of "i" (all equalities and all
704 * inequalities except "k") and the valid constraints of "j" we therefore
705 * obtain a basic map that is equal to their union.
706 * In this case, there is no need to perform a rollback of the tableau
707 * since it is going to be destroyed in fuse().
710 * |\__ |\__
711 * | \__ | \__
712 * | \_ => | \__
713 * |_______| _ |_________\
716 * |\ |\
717 * | \ | \
718 * | \ | \
719 * | | | \
720 * | ||\ => | \
721 * | || \ | \
722 * | || | | |
723 * |__||_/ |_____/
725 static enum isl_change is_adj_ineq_extension(int i, int j,
726 struct isl_coalesce_info *info)
728 int k;
729 struct isl_tab_undo *snap;
730 unsigned n_eq = info[i].bmap->n_eq;
731 unsigned total = isl_basic_map_total_dim(info[i].bmap);
732 isl_stat r;
733 isl_bool super;
735 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
736 return isl_change_error;
738 k = find_ineq(&info[i], STATUS_ADJ_INEQ);
739 if (k < 0)
740 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
741 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
742 return isl_change_error);
744 snap = isl_tab_snap(info[i].tab);
746 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
747 return isl_change_error;
749 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
750 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
751 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
752 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
753 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
754 if (r < 0)
755 return isl_change_error;
757 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
758 if (info[j].ineq[k] != STATUS_VALID)
759 continue;
760 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
761 return isl_change_error;
763 if (isl_tab_detect_constants(info[i].tab) < 0)
764 return isl_change_error;
766 super = contains(&info[j], info[i].tab);
767 if (super < 0)
768 return isl_change_error;
769 if (super)
770 return fuse(i, j, info, NULL, 0, 0);
772 if (isl_tab_rollback(info[i].tab, snap) < 0)
773 return isl_change_error;
775 return isl_change_none;
779 /* Both basic maps have at least one inequality with and adjacent
780 * (but opposite) inequality in the other basic map.
781 * Check that there are no cut constraints and that there is only
782 * a single pair of adjacent inequalities.
783 * If so, we can replace the pair by a single basic map described
784 * by all but the pair of adjacent inequalities.
785 * Any additional points introduced lie strictly between the two
786 * adjacent hyperplanes and can therefore be integral.
788 * ____ _____
789 * / ||\ / \
790 * / || \ / \
791 * \ || \ => \ \
792 * \ || / \ /
793 * \___||_/ \_____/
795 * The test for a single pair of adjancent inequalities is important
796 * for avoiding the combination of two basic maps like the following
798 * /|
799 * / |
800 * /__|
801 * _____
802 * | |
803 * | |
804 * |___|
806 * If there are some cut constraints on one side, then we may
807 * still be able to fuse the two basic maps, but we need to perform
808 * some additional checks in is_adj_ineq_extension.
810 static enum isl_change check_adj_ineq(int i, int j,
811 struct isl_coalesce_info *info)
813 int count_i, count_j;
814 int cut_i, cut_j;
816 count_i = count_ineq(&info[i], STATUS_ADJ_INEQ);
817 count_j = count_ineq(&info[j], STATUS_ADJ_INEQ);
819 if (count_i != 1 && count_j != 1)
820 return isl_change_none;
822 cut_i = any_eq(&info[i], STATUS_CUT) || any_ineq(&info[i], STATUS_CUT);
823 cut_j = any_eq(&info[j], STATUS_CUT) || any_ineq(&info[j], STATUS_CUT);
825 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
826 return fuse(i, j, info, NULL, 0, 0);
828 if (count_i == 1 && !cut_i)
829 return is_adj_ineq_extension(i, j, info);
831 if (count_j == 1 && !cut_j)
832 return is_adj_ineq_extension(j, i, info);
834 return isl_change_none;
837 /* Given an affine transformation matrix "T", does row "row" represent
838 * anything other than a unit vector (possibly shifted by a constant)
839 * that is not involved in any of the other rows?
841 * That is, if a constraint involves the variable corresponding to
842 * the row, then could its preimage by "T" have any coefficients
843 * that are different from those in the original constraint?
845 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
847 int i, j;
848 int len = T->n_col - 1;
850 i = isl_seq_first_non_zero(T->row[row] + 1, len);
851 if (i < 0)
852 return 1;
853 if (!isl_int_is_one(T->row[row][1 + i]) &&
854 !isl_int_is_negone(T->row[row][1 + i]))
855 return 1;
857 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
858 if (j >= 0)
859 return 1;
861 for (j = 1; j < T->n_row; ++j) {
862 if (j == row)
863 continue;
864 if (!isl_int_is_zero(T->row[j][1 + i]))
865 return 1;
868 return 0;
871 /* Does inequality constraint "ineq" of "bmap" involve any of
872 * the variables marked in "affected"?
873 * "total" is the total number of variables, i.e., the number
874 * of entries in "affected".
876 static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
877 int *affected, int total)
879 int i;
881 for (i = 0; i < total; ++i) {
882 if (!affected[i])
883 continue;
884 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
885 return isl_bool_true;
888 return isl_bool_false;
891 /* Given the compressed version of inequality constraint "ineq"
892 * of info->bmap in "v", check if the constraint can be tightened,
893 * where the compression is based on an equality constraint valid
894 * for info->tab.
895 * If so, add the tightened version of the inequality constraint
896 * to info->tab. "v" may be modified by this function.
898 * That is, if the compressed constraint is of the form
900 * m f() + c >= 0
902 * with 0 < c < m, then it is equivalent to
904 * f() >= 0
906 * This means that c can also be subtracted from the original,
907 * uncompressed constraint without affecting the integer points
908 * in info->tab. Add this tightened constraint as an extra row
909 * to info->tab to make this information explicitly available.
911 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
912 int ineq, __isl_take isl_vec *v)
914 isl_ctx *ctx;
915 isl_stat r;
917 if (!v)
918 return NULL;
920 ctx = isl_vec_get_ctx(v);
921 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
922 if (isl_int_is_zero(ctx->normalize_gcd) ||
923 isl_int_is_one(ctx->normalize_gcd)) {
924 return v;
927 v = isl_vec_cow(v);
928 if (!v)
929 return NULL;
931 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
932 if (isl_int_is_zero(v->el[0]))
933 return v;
935 if (isl_tab_extend_cons(info->tab, 1) < 0)
936 return isl_vec_free(v);
938 isl_int_sub(info->bmap->ineq[ineq][0],
939 info->bmap->ineq[ineq][0], v->el[0]);
940 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
941 isl_int_add(info->bmap->ineq[ineq][0],
942 info->bmap->ineq[ineq][0], v->el[0]);
944 if (r < 0)
945 return isl_vec_free(v);
947 return v;
950 /* Tighten the (non-redundant) constraints on the facet represented
951 * by info->tab.
952 * In particular, on input, info->tab represents the result
953 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
954 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
955 * replacing the one at index "l" by the corresponding equality,
956 * i.e., f_k + 1 = 0, with k = relaxed[l].
958 * Compute a variable compression from the equality constraint f_k + 1 = 0
959 * and use it to tighten the other constraints of info->bmap
960 * (that is, all constraints that have not been relaxed),
961 * updating info->tab (and leaving info->bmap untouched).
962 * The compression handles essentially two cases, one where a variable
963 * is assigned a fixed value and can therefore be eliminated, and one
964 * where one variable is a shifted multiple of some other variable and
965 * can therefore be replaced by that multiple.
966 * Gaussian elimination would also work for the first case, but for
967 * the second case, the effectiveness would depend on the order
968 * of the variables.
969 * After compression, some of the constraints may have coefficients
970 * with a common divisor. If this divisor does not divide the constant
971 * term, then the constraint can be tightened.
972 * The tightening is performed on the tableau info->tab by introducing
973 * extra (temporary) constraints.
975 * Only constraints that are possibly affected by the compression are
976 * considered. In particular, if the constraint only involves variables
977 * that are directly mapped to a distinct set of other variables, then
978 * no common divisor can be introduced and no tightening can occur.
980 * It is important to only consider the non-redundant constraints
981 * since the facet constraint has been relaxed prior to the call
982 * to this function, meaning that the constraints that were redundant
983 * prior to the relaxation may no longer be redundant.
984 * These constraints will be ignored in the fused result, so
985 * the fusion detection should not exploit them.
987 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
988 int n, int *relaxed, int l)
990 unsigned total;
991 isl_ctx *ctx;
992 isl_vec *v = NULL;
993 isl_mat *T;
994 int i;
995 int k;
996 int *affected;
998 k = relaxed[l];
999 ctx = isl_basic_map_get_ctx(info->bmap);
1000 total = isl_basic_map_total_dim(info->bmap);
1001 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
1002 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
1003 T = isl_mat_variable_compression(T, NULL);
1004 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
1005 if (!T)
1006 return isl_stat_error;
1007 if (T->n_col == 0) {
1008 isl_mat_free(T);
1009 return isl_stat_ok;
1012 affected = isl_alloc_array(ctx, int, total);
1013 if (!affected)
1014 goto error;
1016 for (i = 0; i < total; ++i)
1017 affected[i] = not_unique_unit_row(T, 1 + i);
1019 for (i = 0; i < info->bmap->n_ineq; ++i) {
1020 isl_bool handle;
1021 if (any(relaxed, n, i))
1022 continue;
1023 if (info->ineq[i] == STATUS_REDUNDANT)
1024 continue;
1025 handle = is_affected(info->bmap, i, affected, total);
1026 if (handle < 0)
1027 goto error;
1028 if (!handle)
1029 continue;
1030 v = isl_vec_alloc(ctx, 1 + total);
1031 if (!v)
1032 goto error;
1033 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
1034 v = isl_vec_mat_product(v, isl_mat_copy(T));
1035 v = try_tightening(info, i, v);
1036 isl_vec_free(v);
1037 if (!v)
1038 goto error;
1041 isl_mat_free(T);
1042 free(affected);
1043 return isl_stat_ok;
1044 error:
1045 isl_mat_free(T);
1046 free(affected);
1047 return isl_stat_error;
1050 /* Replace the basic maps "i" and "j" by an extension of "i"
1051 * along the "n" inequality constraints in "relax" by one.
1052 * The tableau info[i].tab has already been extended.
1053 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
1054 * by one.
1055 * Each integer division that does not have exactly the same
1056 * definition in "i" and "j" is marked unknown and the basic map
1057 * is scheduled to be simplified in an attempt to recover
1058 * the integer division definition.
1059 * Place the extension in the position that is the smallest of i and j.
1061 static enum isl_change extend(int i, int j, int n, int *relax,
1062 struct isl_coalesce_info *info)
1064 int l;
1065 unsigned total;
1067 info[i].bmap = isl_basic_map_cow(info[i].bmap);
1068 if (!info[i].bmap)
1069 return isl_change_error;
1070 total = isl_basic_map_total_dim(info[i].bmap);
1071 for (l = 0; l < info[i].bmap->n_div; ++l)
1072 if (!isl_seq_eq(info[i].bmap->div[l],
1073 info[j].bmap->div[l], 1 + 1 + total)) {
1074 isl_int_set_si(info[i].bmap->div[l][0], 0);
1075 info[i].simplify = 1;
1077 for (l = 0; l < n; ++l)
1078 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
1079 info[i].bmap->ineq[relax[l]][0], 1);
1080 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1081 drop(&info[j]);
1082 if (j < i)
1083 exchange(&info[i], &info[j]);
1084 return isl_change_fuse;
1087 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1088 * that are such that they include basic map "j" if they are relaxed
1089 * by one. All the other inequalities are valid for "j".
1090 * Check if basic map "j" forms an extension of basic map "i".
1092 * In particular, relax the constraints in "relax", compute the corresponding
1093 * facets one by one and check whether each of these is included
1094 * in the other basic map.
1095 * Before testing for inclusion, the constraints on each facet
1096 * are tightened to increase the chance of an inclusion being detected.
1097 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1098 * in is_adj_ineq_extension, may further increase those chances, but this
1099 * is not currently done.)
1100 * If each facet is included, we know that relaxing the constraints extends
1101 * the basic map with exactly the other basic map (we already know that this
1102 * other basic map is included in the extension, because all other
1103 * inequality constraints are valid of "j") and we can replace the
1104 * two basic maps by this extension.
1106 * If any of the relaxed constraints turn out to be redundant, then bail out.
1107 * isl_tab_select_facet refuses to handle such constraints. It may be
1108 * possible to handle them anyway by making a distinction between
1109 * redundant constraints with a corresponding facet that still intersects
1110 * the set (allowing isl_tab_select_facet to handle them) and
1111 * those where the facet does not intersect the set (which can be ignored
1112 * because the empty facet is trivially included in the other disjunct).
1113 * However, relaxed constraints that turn out to be redundant should
1114 * be fairly rare and no such instance has been reported where
1115 * coalescing would be successful.
1116 * ____ _____
1117 * / || / |
1118 * / || / |
1119 * \ || => \ |
1120 * \ || \ |
1121 * \___|| \____|
1124 * \ |\
1125 * |\\ | \
1126 * | \\ | \
1127 * | | => | /
1128 * | / | /
1129 * |/ |/
1131 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1132 struct isl_coalesce_info *info)
1134 int l;
1135 isl_bool super;
1136 struct isl_tab_undo *snap, *snap2;
1137 unsigned n_eq = info[i].bmap->n_eq;
1139 for (l = 0; l < n; ++l)
1140 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1141 return isl_change_none;
1143 snap = isl_tab_snap(info[i].tab);
1144 for (l = 0; l < n; ++l)
1145 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1146 return isl_change_error;
1147 for (l = 0; l < n; ++l) {
1148 if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
1149 continue;
1150 if (isl_tab_rollback(info[i].tab, snap) < 0)
1151 return isl_change_error;
1152 return isl_change_none;
1154 snap2 = isl_tab_snap(info[i].tab);
1155 for (l = 0; l < n; ++l) {
1156 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1157 return isl_change_error;
1158 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1159 return isl_change_error;
1160 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1161 return isl_change_error;
1162 super = contains(&info[j], info[i].tab);
1163 if (super < 0)
1164 return isl_change_error;
1165 if (super)
1166 continue;
1167 if (isl_tab_rollback(info[i].tab, snap) < 0)
1168 return isl_change_error;
1169 return isl_change_none;
1172 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1173 return isl_change_error;
1174 return extend(i, j, n, relax, info);
1177 /* Data structure that keeps track of the wrapping constraints
1178 * and of information to bound the coefficients of those constraints.
1180 * bound is set if we want to apply a bound on the coefficients
1181 * mat contains the wrapping constraints
1182 * max is the bound on the coefficients (if bound is set)
1184 struct isl_wraps {
1185 int bound;
1186 isl_mat *mat;
1187 isl_int max;
1190 /* Update wraps->max to be greater than or equal to the coefficients
1191 * in the equalities and inequalities of info->bmap that can be removed
1192 * if we end up applying wrapping.
1194 static isl_stat wraps_update_max(struct isl_wraps *wraps,
1195 struct isl_coalesce_info *info)
1197 int k;
1198 isl_int max_k;
1199 unsigned total = isl_basic_map_total_dim(info->bmap);
1201 isl_int_init(max_k);
1203 for (k = 0; k < info->bmap->n_eq; ++k) {
1204 if (info->eq[2 * k] == STATUS_VALID &&
1205 info->eq[2 * k + 1] == STATUS_VALID)
1206 continue;
1207 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1208 if (isl_int_abs_gt(max_k, wraps->max))
1209 isl_int_set(wraps->max, max_k);
1212 for (k = 0; k < info->bmap->n_ineq; ++k) {
1213 if (info->ineq[k] == STATUS_VALID ||
1214 info->ineq[k] == STATUS_REDUNDANT)
1215 continue;
1216 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1217 if (isl_int_abs_gt(max_k, wraps->max))
1218 isl_int_set(wraps->max, max_k);
1221 isl_int_clear(max_k);
1223 return isl_stat_ok;
1226 /* Initialize the isl_wraps data structure.
1227 * If we want to bound the coefficients of the wrapping constraints,
1228 * we set wraps->max to the largest coefficient
1229 * in the equalities and inequalities that can be removed if we end up
1230 * applying wrapping.
1232 static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1233 struct isl_coalesce_info *info, int i, int j)
1235 isl_ctx *ctx;
1237 wraps->bound = 0;
1238 wraps->mat = mat;
1239 if (!mat)
1240 return isl_stat_error;
1241 ctx = isl_mat_get_ctx(mat);
1242 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1243 if (!wraps->bound)
1244 return isl_stat_ok;
1245 isl_int_init(wraps->max);
1246 isl_int_set_si(wraps->max, 0);
1247 if (wraps_update_max(wraps, &info[i]) < 0)
1248 return isl_stat_error;
1249 if (wraps_update_max(wraps, &info[j]) < 0)
1250 return isl_stat_error;
1252 return isl_stat_ok;
1255 /* Free the contents of the isl_wraps data structure.
1257 static void wraps_free(struct isl_wraps *wraps)
1259 isl_mat_free(wraps->mat);
1260 if (wraps->bound)
1261 isl_int_clear(wraps->max);
1264 /* Is the wrapping constraint in row "row" allowed?
1266 * If wraps->bound is set, we check that none of the coefficients
1267 * is greater than wraps->max.
1269 static int allow_wrap(struct isl_wraps *wraps, int row)
1271 int i;
1273 if (!wraps->bound)
1274 return 1;
1276 for (i = 1; i < wraps->mat->n_col; ++i)
1277 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1278 return 0;
1280 return 1;
1283 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1284 * to include "set" and add the result in position "w" of "wraps".
1285 * "len" is the total number of coefficients in "bound" and "ineq".
1286 * Return 1 on success, 0 on failure and -1 on error.
1287 * Wrapping can fail if the result of wrapping is equal to "bound"
1288 * or if we want to bound the sizes of the coefficients and
1289 * the wrapped constraint does not satisfy this bound.
1291 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1292 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1294 isl_seq_cpy(wraps->mat->row[w], bound, len);
1295 if (negate) {
1296 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1297 ineq = wraps->mat->row[w + 1];
1299 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1300 return -1;
1301 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1302 return 0;
1303 if (!allow_wrap(wraps, w))
1304 return 0;
1305 return 1;
1308 /* For each constraint in info->bmap that is not redundant (as determined
1309 * by info->tab) and that is not a valid constraint for the other basic map,
1310 * wrap the constraint around "bound" such that it includes the whole
1311 * set "set" and append the resulting constraint to "wraps".
1312 * Note that the constraints that are valid for the other basic map
1313 * will be added to the combined basic map by default, so there is
1314 * no need to wrap them.
1315 * The caller wrap_in_facets even relies on this function not wrapping
1316 * any constraints that are already valid.
1317 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1318 * wraps->n_row is the number of actual wrapped constraints that have
1319 * been added.
1320 * If any of the wrapping problems results in a constraint that is
1321 * identical to "bound", then this means that "set" is unbounded in such
1322 * way that no wrapping is possible. If this happens then wraps->n_row
1323 * is reset to zero.
1324 * Similarly, if we want to bound the coefficients of the wrapping
1325 * constraints and a newly added wrapping constraint does not
1326 * satisfy the bound, then wraps->n_row is also reset to zero.
1328 static isl_stat add_wraps(struct isl_wraps *wraps,
1329 struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
1331 int l, m;
1332 int w;
1333 int added;
1334 isl_basic_map *bmap = info->bmap;
1335 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1337 w = wraps->mat->n_row;
1339 for (l = 0; l < bmap->n_ineq; ++l) {
1340 if (info->ineq[l] == STATUS_VALID ||
1341 info->ineq[l] == STATUS_REDUNDANT)
1342 continue;
1343 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1344 continue;
1345 if (isl_seq_eq(bound, bmap->ineq[l], len))
1346 continue;
1347 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1348 continue;
1350 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1351 if (added < 0)
1352 return isl_stat_error;
1353 if (!added)
1354 goto unbounded;
1355 ++w;
1357 for (l = 0; l < bmap->n_eq; ++l) {
1358 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1359 continue;
1360 if (isl_seq_eq(bound, bmap->eq[l], len))
1361 continue;
1363 for (m = 0; m < 2; ++m) {
1364 if (info->eq[2 * l + m] == STATUS_VALID)
1365 continue;
1366 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1367 set, !m);
1368 if (added < 0)
1369 return isl_stat_error;
1370 if (!added)
1371 goto unbounded;
1372 ++w;
1376 wraps->mat->n_row = w;
1377 return isl_stat_ok;
1378 unbounded:
1379 wraps->mat->n_row = 0;
1380 return isl_stat_ok;
1383 /* Check if the constraints in "wraps" from "first" until the last
1384 * are all valid for the basic set represented by "tab".
1385 * If not, wraps->n_row is set to zero.
1387 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1388 struct isl_tab *tab)
1390 int i;
1392 for (i = first; i < wraps->n_row; ++i) {
1393 enum isl_ineq_type type;
1394 type = isl_tab_ineq_type(tab, wraps->row[i]);
1395 if (type == isl_ineq_error)
1396 return -1;
1397 if (type == isl_ineq_redundant)
1398 continue;
1399 wraps->n_row = 0;
1400 return 0;
1403 return 0;
1406 /* Return a set that corresponds to the non-redundant constraints
1407 * (as recorded in tab) of bmap.
1409 * It's important to remove the redundant constraints as some
1410 * of the other constraints may have been modified after the
1411 * constraints were marked redundant.
1412 * In particular, a constraint may have been relaxed.
1413 * Redundant constraints are ignored when a constraint is relaxed
1414 * and should therefore continue to be ignored ever after.
1415 * Otherwise, the relaxation might be thwarted by some of
1416 * these constraints.
1418 * Update the underlying set to ensure that the dimension doesn't change.
1419 * Otherwise the integer divisions could get dropped if the tab
1420 * turns out to be empty.
1422 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1423 struct isl_tab *tab)
1425 isl_basic_set *bset;
1427 bmap = isl_basic_map_copy(bmap);
1428 bset = isl_basic_map_underlying_set(bmap);
1429 bset = isl_basic_set_cow(bset);
1430 bset = isl_basic_set_update_from_tab(bset, tab);
1431 return isl_set_from_basic_set(bset);
1434 /* Wrap the constraints of info->bmap that bound the facet defined
1435 * by inequality "k" around (the opposite of) this inequality to
1436 * include "set". "bound" may be used to store the negated inequality.
1437 * Since the wrapped constraints are not guaranteed to contain the whole
1438 * of info->bmap, we check them in check_wraps.
1439 * If any of the wrapped constraints turn out to be invalid, then
1440 * check_wraps will reset wrap->n_row to zero.
1442 static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
1443 struct isl_coalesce_info *info, int k, isl_int *bound,
1444 __isl_keep isl_set *set)
1446 struct isl_tab_undo *snap;
1447 int n;
1448 unsigned total = isl_basic_map_total_dim(info->bmap);
1450 snap = isl_tab_snap(info->tab);
1452 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1453 return isl_stat_error;
1454 if (isl_tab_detect_redundant(info->tab) < 0)
1455 return isl_stat_error;
1457 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1459 n = wraps->mat->n_row;
1460 if (add_wraps(wraps, info, bound, set) < 0)
1461 return isl_stat_error;
1463 if (isl_tab_rollback(info->tab, snap) < 0)
1464 return isl_stat_error;
1465 if (check_wraps(wraps->mat, n, info->tab) < 0)
1466 return isl_stat_error;
1468 return isl_stat_ok;
1471 /* Given a basic set i with a constraint k that is adjacent to
1472 * basic set j, check if we can wrap
1473 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1474 * (always) around their ridges to include the other set.
1475 * If so, replace the pair of basic sets by their union.
1477 * All constraints of i (except k) are assumed to be valid or
1478 * cut constraints for j.
1479 * Wrapping the cut constraints to include basic map j may result
1480 * in constraints that are no longer valid of basic map i
1481 * we have to check that the resulting wrapping constraints are valid for i.
1482 * If "wrap_facet" is not set, then all constraints of i (except k)
1483 * are assumed to be valid for j.
1484 * ____ _____
1485 * / | / \
1486 * / || / |
1487 * \ || => \ |
1488 * \ || \ |
1489 * \___|| \____|
1492 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1493 struct isl_coalesce_info *info, int wrap_facet)
1495 enum isl_change change = isl_change_none;
1496 struct isl_wraps wraps;
1497 isl_ctx *ctx;
1498 isl_mat *mat;
1499 struct isl_set *set_i = NULL;
1500 struct isl_set *set_j = NULL;
1501 struct isl_vec *bound = NULL;
1502 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1504 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1505 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1506 ctx = isl_basic_map_get_ctx(info[i].bmap);
1507 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1508 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1509 1 + total);
1510 if (wraps_init(&wraps, mat, info, i, j) < 0)
1511 goto error;
1512 bound = isl_vec_alloc(ctx, 1 + total);
1513 if (!set_i || !set_j || !bound)
1514 goto error;
1516 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1517 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1518 isl_seq_normalize(ctx, bound->el, 1 + total);
1520 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1521 wraps.mat->n_row = 1;
1523 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1524 goto error;
1525 if (!wraps.mat->n_row)
1526 goto unbounded;
1528 if (wrap_facet) {
1529 if (add_wraps_around_facet(&wraps, &info[i], k,
1530 bound->el, set_j) < 0)
1531 goto error;
1532 if (!wraps.mat->n_row)
1533 goto unbounded;
1536 change = fuse(i, j, info, wraps.mat, 0, 0);
1538 unbounded:
1539 wraps_free(&wraps);
1541 isl_set_free(set_i);
1542 isl_set_free(set_j);
1544 isl_vec_free(bound);
1546 return change;
1547 error:
1548 wraps_free(&wraps);
1549 isl_vec_free(bound);
1550 isl_set_free(set_i);
1551 isl_set_free(set_j);
1552 return isl_change_error;
1555 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1556 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1557 * add wrapping constraints to wrap.mat for all constraints
1558 * of basic map j that bound the part of basic map j that sticks out
1559 * of the cut constraint.
1560 * "set_i" is the underlying set of basic map i.
1561 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1563 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1564 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1565 * (with respect to the integer points), so we add t(x) >= 0 instead.
1566 * Otherwise, we wrap the constraints of basic map j that are not
1567 * redundant in this intersection and that are not already valid
1568 * for basic map i over basic map i.
1569 * Note that it is sufficient to wrap the constraints to include
1570 * basic map i, because we will only wrap the constraints that do
1571 * not include basic map i already. The wrapped constraint will
1572 * therefore be more relaxed compared to the original constraint.
1573 * Since the original constraint is valid for basic map j, so is
1574 * the wrapped constraint.
1576 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1577 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1578 struct isl_tab_undo *snap)
1580 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1581 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1582 return isl_stat_error;
1583 if (isl_tab_detect_redundant(info_j->tab) < 0)
1584 return isl_stat_error;
1586 if (info_j->tab->empty)
1587 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1588 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1589 return isl_stat_error;
1591 if (isl_tab_rollback(info_j->tab, snap) < 0)
1592 return isl_stat_error;
1594 return isl_stat_ok;
1597 /* Given a pair of basic maps i and j such that j sticks out
1598 * of i at n cut constraints, each time by at most one,
1599 * try to compute wrapping constraints and replace the two
1600 * basic maps by a single basic map.
1601 * The other constraints of i are assumed to be valid for j.
1602 * "set_i" is the underlying set of basic map i.
1603 * "wraps" has been initialized to be of the right size.
1605 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1606 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1607 * of basic map j that bound the part of basic map j that sticks out
1608 * of the cut constraint.
1610 * If any wrapping fails, i.e., if we cannot wrap to touch
1611 * the union, then we give up.
1612 * Otherwise, the pair of basic maps is replaced by their union.
1614 static enum isl_change try_wrap_in_facets(int i, int j,
1615 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1616 __isl_keep isl_set *set_i)
1618 int k, l, w;
1619 unsigned total;
1620 struct isl_tab_undo *snap;
1622 total = isl_basic_map_total_dim(info[i].bmap);
1624 snap = isl_tab_snap(info[j].tab);
1626 wraps->mat->n_row = 0;
1628 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1629 for (l = 0; l < 2; ++l) {
1630 if (info[i].eq[2 * k + l] != STATUS_CUT)
1631 continue;
1632 w = wraps->mat->n_row++;
1633 if (l == 0)
1634 isl_seq_neg(wraps->mat->row[w],
1635 info[i].bmap->eq[k], 1 + total);
1636 else
1637 isl_seq_cpy(wraps->mat->row[w],
1638 info[i].bmap->eq[k], 1 + total);
1639 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1640 return isl_change_error;
1642 if (!wraps->mat->n_row)
1643 return isl_change_none;
1647 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1648 if (info[i].ineq[k] != STATUS_CUT)
1649 continue;
1650 w = wraps->mat->n_row++;
1651 isl_seq_cpy(wraps->mat->row[w],
1652 info[i].bmap->ineq[k], 1 + total);
1653 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1654 return isl_change_error;
1656 if (!wraps->mat->n_row)
1657 return isl_change_none;
1660 return fuse(i, j, info, wraps->mat, 0, 1);
1663 /* Given a pair of basic maps i and j such that j sticks out
1664 * of i at n cut constraints, each time by at most one,
1665 * try to compute wrapping constraints and replace the two
1666 * basic maps by a single basic map.
1667 * The other constraints of i are assumed to be valid for j.
1669 * The core computation is performed by try_wrap_in_facets.
1670 * This function simply extracts an underlying set representation
1671 * of basic map i and initializes the data structure for keeping
1672 * track of wrapping constraints.
1674 static enum isl_change wrap_in_facets(int i, int j, int n,
1675 struct isl_coalesce_info *info)
1677 enum isl_change change = isl_change_none;
1678 struct isl_wraps wraps;
1679 isl_ctx *ctx;
1680 isl_mat *mat;
1681 isl_set *set_i = NULL;
1682 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1683 int max_wrap;
1685 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1686 return isl_change_error;
1688 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1689 max_wrap *= n;
1691 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1692 ctx = isl_basic_map_get_ctx(info[i].bmap);
1693 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1694 if (wraps_init(&wraps, mat, info, i, j) < 0)
1695 goto error;
1696 if (!set_i)
1697 goto error;
1699 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1701 wraps_free(&wraps);
1702 isl_set_free(set_i);
1704 return change;
1705 error:
1706 wraps_free(&wraps);
1707 isl_set_free(set_i);
1708 return isl_change_error;
1711 /* Return the effect of inequality "ineq" on the tableau "tab",
1712 * after relaxing the constant term of "ineq" by one.
1714 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1716 enum isl_ineq_type type;
1718 isl_int_add_ui(ineq[0], ineq[0], 1);
1719 type = isl_tab_ineq_type(tab, ineq);
1720 isl_int_sub_ui(ineq[0], ineq[0], 1);
1722 return type;
1725 /* Given two basic sets i and j,
1726 * check if relaxing all the cut constraints of i by one turns
1727 * them into valid constraint for j and check if we can wrap in
1728 * the bits that are sticking out.
1729 * If so, replace the pair by their union.
1731 * We first check if all relaxed cut inequalities of i are valid for j
1732 * and then try to wrap in the intersections of the relaxed cut inequalities
1733 * with j.
1735 * During this wrapping, we consider the points of j that lie at a distance
1736 * of exactly 1 from i. In particular, we ignore the points that lie in
1737 * between this lower-dimensional space and the basic map i.
1738 * We can therefore only apply this to integer maps.
1739 * ____ _____
1740 * / ___|_ / \
1741 * / | | / |
1742 * \ | | => \ |
1743 * \|____| \ |
1744 * \___| \____/
1746 * _____ ______
1747 * | ____|_ | \
1748 * | | | | |
1749 * | | | => | |
1750 * |_| | | |
1751 * |_____| \______|
1753 * _______
1754 * | |
1755 * | |\ |
1756 * | | \ |
1757 * | | \ |
1758 * | | \|
1759 * | | \
1760 * | |_____\
1761 * | |
1762 * |_______|
1764 * Wrapping can fail if the result of wrapping one of the facets
1765 * around its edges does not produce any new facet constraint.
1766 * In particular, this happens when we try to wrap in unbounded sets.
1768 * _______________________________________________________________________
1770 * | ___
1771 * | | |
1772 * |_| |_________________________________________________________________
1773 * |___|
1775 * The following is not an acceptable result of coalescing the above two
1776 * sets as it includes extra integer points.
1777 * _______________________________________________________________________
1779 * |
1780 * |
1782 * \______________________________________________________________________
1784 static enum isl_change can_wrap_in_set(int i, int j,
1785 struct isl_coalesce_info *info)
1787 int k, l;
1788 int n;
1789 unsigned total;
1791 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1792 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1793 return isl_change_none;
1795 n = count_eq(&info[i], STATUS_CUT) + count_ineq(&info[i], STATUS_CUT);
1796 if (n == 0)
1797 return isl_change_none;
1799 total = isl_basic_map_total_dim(info[i].bmap);
1800 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1801 for (l = 0; l < 2; ++l) {
1802 enum isl_ineq_type type;
1804 if (info[i].eq[2 * k + l] != STATUS_CUT)
1805 continue;
1807 if (l == 0)
1808 isl_seq_neg(info[i].bmap->eq[k],
1809 info[i].bmap->eq[k], 1 + total);
1810 type = type_of_relaxed(info[j].tab,
1811 info[i].bmap->eq[k]);
1812 if (l == 0)
1813 isl_seq_neg(info[i].bmap->eq[k],
1814 info[i].bmap->eq[k], 1 + total);
1815 if (type == isl_ineq_error)
1816 return isl_change_error;
1817 if (type != isl_ineq_redundant)
1818 return isl_change_none;
1822 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1823 enum isl_ineq_type type;
1825 if (info[i].ineq[k] != STATUS_CUT)
1826 continue;
1828 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1829 if (type == isl_ineq_error)
1830 return isl_change_error;
1831 if (type != isl_ineq_redundant)
1832 return isl_change_none;
1835 return wrap_in_facets(i, j, n, info);
1838 /* Check if either i or j has only cut constraints that can
1839 * be used to wrap in (a facet of) the other basic set.
1840 * if so, replace the pair by their union.
1842 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1844 enum isl_change change = isl_change_none;
1846 change = can_wrap_in_set(i, j, info);
1847 if (change != isl_change_none)
1848 return change;
1850 change = can_wrap_in_set(j, i, info);
1851 return change;
1854 /* Check if all inequality constraints of "i" that cut "j" cease
1855 * to be cut constraints if they are relaxed by one.
1856 * If so, collect the cut constraints in "list".
1857 * The caller is responsible for allocating "list".
1859 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1860 int *list)
1862 int l, n;
1864 n = 0;
1865 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1866 enum isl_ineq_type type;
1868 if (info[i].ineq[l] != STATUS_CUT)
1869 continue;
1870 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1871 if (type == isl_ineq_error)
1872 return isl_bool_error;
1873 if (type != isl_ineq_redundant)
1874 return isl_bool_false;
1875 list[n++] = l;
1878 return isl_bool_true;
1881 /* Given two basic maps such that "j" has at least one equality constraint
1882 * that is adjacent to an inequality constraint of "i" and such that "i" has
1883 * exactly one inequality constraint that is adjacent to an equality
1884 * constraint of "j", check whether "i" can be extended to include "j" or
1885 * whether "j" can be wrapped into "i".
1886 * All remaining constraints of "i" and "j" are assumed to be valid
1887 * or cut constraints of the other basic map.
1888 * However, none of the equality constraints of "i" are cut constraints.
1890 * If "i" has any "cut" inequality constraints, then check if relaxing
1891 * each of them by one is sufficient for them to become valid.
1892 * If so, check if the inequality constraint adjacent to an equality
1893 * constraint of "j" along with all these cut constraints
1894 * can be relaxed by one to contain exactly "j".
1895 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1897 static enum isl_change check_single_adj_eq(int i, int j,
1898 struct isl_coalesce_info *info)
1900 enum isl_change change = isl_change_none;
1901 int k;
1902 int n_cut;
1903 int *relax;
1904 isl_ctx *ctx;
1905 isl_bool try_relax;
1907 n_cut = count_ineq(&info[i], STATUS_CUT);
1909 k = find_ineq(&info[i], STATUS_ADJ_EQ);
1911 if (n_cut > 0) {
1912 ctx = isl_basic_map_get_ctx(info[i].bmap);
1913 relax = isl_calloc_array(ctx, int, 1 + n_cut);
1914 if (!relax)
1915 return isl_change_error;
1916 relax[0] = k;
1917 try_relax = all_cut_by_one(i, j, info, relax + 1);
1918 if (try_relax < 0)
1919 change = isl_change_error;
1920 } else {
1921 try_relax = isl_bool_true;
1922 relax = &k;
1924 if (try_relax && change == isl_change_none)
1925 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
1926 if (n_cut > 0)
1927 free(relax);
1928 if (change != isl_change_none)
1929 return change;
1931 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
1933 return change;
1936 /* At least one of the basic maps has an equality that is adjacent
1937 * to an inequality. Make sure that only one of the basic maps has
1938 * such an equality and that the other basic map has exactly one
1939 * inequality adjacent to an equality.
1940 * If the other basic map does not have such an inequality, then
1941 * check if all its constraints are either valid or cut constraints
1942 * and, if so, try wrapping in the first map into the second.
1943 * Otherwise, try to extend one basic map with the other or
1944 * wrap one basic map in the other.
1946 static enum isl_change check_adj_eq(int i, int j,
1947 struct isl_coalesce_info *info)
1949 if (any_eq(&info[i], STATUS_ADJ_INEQ) &&
1950 any_eq(&info[j], STATUS_ADJ_INEQ))
1951 /* ADJ EQ TOO MANY */
1952 return isl_change_none;
1954 if (any_eq(&info[i], STATUS_ADJ_INEQ))
1955 return check_adj_eq(j, i, info);
1957 /* j has an equality adjacent to an inequality in i */
1959 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1) {
1960 if (all_valid_or_cut(&info[i]))
1961 return can_wrap_in_set(i, j, info);
1962 return isl_change_none;
1964 if (any_eq(&info[i], STATUS_CUT))
1965 return isl_change_none;
1966 if (any_ineq(&info[j], STATUS_ADJ_EQ) ||
1967 any_ineq(&info[i], STATUS_ADJ_INEQ) ||
1968 any_ineq(&info[j], STATUS_ADJ_INEQ))
1969 /* ADJ EQ TOO MANY */
1970 return isl_change_none;
1972 return check_single_adj_eq(i, j, info);
1975 /* Disjunct "j" lies on a hyperplane that is adjacent to disjunct "i".
1976 * In particular, disjunct "i" has an inequality constraint that is adjacent
1977 * to a (combination of) equality constraint(s) of disjunct "j",
1978 * but disjunct "j" has no explicit equality constraint adjacent
1979 * to an inequality constraint of disjunct "i".
1981 * Disjunct "i" is already known not to have any equality constraints
1982 * that are adjacent to an equality or inequality constraint.
1983 * Check that, other than the inequality constraint mentioned above,
1984 * all other constraints of disjunct "i" are valid for disjunct "j".
1985 * If so, try and wrap in disjunct "j".
1987 static enum isl_change check_ineq_adj_eq(int i, int j,
1988 struct isl_coalesce_info *info)
1990 int k;
1992 if (any_eq(&info[i], STATUS_CUT))
1993 return isl_change_none;
1994 if (any_ineq(&info[i], STATUS_CUT))
1995 return isl_change_none;
1996 if (any_ineq(&info[i], STATUS_ADJ_INEQ))
1997 return isl_change_none;
1998 if (count_ineq(&info[i], STATUS_ADJ_EQ) != 1)
1999 return isl_change_none;
2001 k = find_ineq(&info[i], STATUS_ADJ_EQ);
2003 return can_wrap_in_facet(i, j, k, info, 0);
2006 /* The two basic maps lie on adjacent hyperplanes. In particular,
2007 * basic map "i" has an equality that lies parallel to basic map "j".
2008 * Check if we can wrap the facets around the parallel hyperplanes
2009 * to include the other set.
2011 * We perform basically the same operations as can_wrap_in_facet,
2012 * except that we don't need to select a facet of one of the sets.
2014 * \\ \\
2015 * \\ => \\
2016 * \ \|
2018 * If there is more than one equality of "i" adjacent to an equality of "j",
2019 * then the result will satisfy one or more equalities that are a linear
2020 * combination of these equalities. These will be encoded as pairs
2021 * of inequalities in the wrapping constraints and need to be made
2022 * explicit.
2024 static enum isl_change check_eq_adj_eq(int i, int j,
2025 struct isl_coalesce_info *info)
2027 int k;
2028 enum isl_change change = isl_change_none;
2029 int detect_equalities = 0;
2030 struct isl_wraps wraps;
2031 isl_ctx *ctx;
2032 isl_mat *mat;
2033 struct isl_set *set_i = NULL;
2034 struct isl_set *set_j = NULL;
2035 struct isl_vec *bound = NULL;
2036 unsigned total = isl_basic_map_total_dim(info[i].bmap);
2038 if (count_eq(&info[i], STATUS_ADJ_EQ) != 1)
2039 detect_equalities = 1;
2041 k = find_eq(&info[i], STATUS_ADJ_EQ);
2043 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
2044 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
2045 ctx = isl_basic_map_get_ctx(info[i].bmap);
2046 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
2047 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
2048 1 + total);
2049 if (wraps_init(&wraps, mat, info, i, j) < 0)
2050 goto error;
2051 bound = isl_vec_alloc(ctx, 1 + total);
2052 if (!set_i || !set_j || !bound)
2053 goto error;
2055 if (k % 2 == 0)
2056 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2057 else
2058 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
2059 isl_int_add_ui(bound->el[0], bound->el[0], 1);
2061 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
2062 wraps.mat->n_row = 1;
2064 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
2065 goto error;
2066 if (!wraps.mat->n_row)
2067 goto unbounded;
2069 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
2070 isl_seq_neg(bound->el, bound->el, 1 + total);
2072 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
2073 wraps.mat->n_row++;
2075 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
2076 goto error;
2077 if (!wraps.mat->n_row)
2078 goto unbounded;
2080 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
2082 if (0) {
2083 error: change = isl_change_error;
2085 unbounded:
2087 wraps_free(&wraps);
2088 isl_set_free(set_i);
2089 isl_set_free(set_j);
2090 isl_vec_free(bound);
2092 return change;
2095 /* Initialize the "eq" and "ineq" fields of "info".
2097 static void init_status(struct isl_coalesce_info *info)
2099 info->eq = info->ineq = NULL;
2102 /* Set info->eq to the positions of the equalities of info->bmap
2103 * with respect to the basic map represented by "tab".
2104 * If info->eq has already been computed, then do not compute it again.
2106 static void set_eq_status_in(struct isl_coalesce_info *info,
2107 struct isl_tab *tab)
2109 if (info->eq)
2110 return;
2111 info->eq = eq_status_in(info->bmap, tab);
2114 /* Set info->ineq to the positions of the inequalities of info->bmap
2115 * with respect to the basic map represented by "tab".
2116 * If info->ineq has already been computed, then do not compute it again.
2118 static void set_ineq_status_in(struct isl_coalesce_info *info,
2119 struct isl_tab *tab)
2121 if (info->ineq)
2122 return;
2123 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
2126 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2127 * This function assumes that init_status has been called on "info" first,
2128 * after which the "eq" and "ineq" fields may or may not have been
2129 * assigned a newly allocated array.
2131 static void clear_status(struct isl_coalesce_info *info)
2133 free(info->eq);
2134 free(info->ineq);
2137 /* Are all inequality constraints of the basic map represented by "info"
2138 * valid for the other basic map, except for a single constraint
2139 * that is adjacent to an inequality constraint of the other basic map?
2141 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2143 int i;
2144 int k = -1;
2146 for (i = 0; i < info->bmap->n_ineq; ++i) {
2147 if (info->ineq[i] == STATUS_REDUNDANT)
2148 continue;
2149 if (info->ineq[i] == STATUS_VALID)
2150 continue;
2151 if (info->ineq[i] != STATUS_ADJ_INEQ)
2152 return 0;
2153 if (k != -1)
2154 return 0;
2155 k = i;
2158 return k != -1;
2161 /* Basic map "i" has one or more equality constraints that separate it
2162 * from basic map "j". Check if it happens to be an extension
2163 * of basic map "j".
2164 * In particular, check that all constraints of "j" are valid for "i",
2165 * except for one inequality constraint that is adjacent
2166 * to an inequality constraints of "i".
2167 * If so, check for "i" being an extension of "j" by calling
2168 * is_adj_ineq_extension.
2170 * Clean up the memory allocated for keeping track of the status
2171 * of the constraints before returning.
2173 static enum isl_change separating_equality(int i, int j,
2174 struct isl_coalesce_info *info)
2176 enum isl_change change = isl_change_none;
2178 if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2179 all_ineq_valid_or_single_adj_ineq(&info[j]))
2180 change = is_adj_ineq_extension(j, i, info);
2182 clear_status(&info[i]);
2183 clear_status(&info[j]);
2184 return change;
2187 /* Check if the union of the given pair of basic maps
2188 * can be represented by a single basic map.
2189 * If so, replace the pair by the single basic map and return
2190 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2191 * Otherwise, return isl_change_none.
2192 * The two basic maps are assumed to live in the same local space.
2193 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2194 * to have been initialized by the caller, either to NULL or
2195 * to valid information.
2197 * We first check the effect of each constraint of one basic map
2198 * on the other basic map.
2199 * The constraint may be
2200 * redundant the constraint is redundant in its own
2201 * basic map and should be ignore and removed
2202 * in the end
2203 * valid all (integer) points of the other basic map
2204 * satisfy the constraint
2205 * separate no (integer) point of the other basic map
2206 * satisfies the constraint
2207 * cut some but not all points of the other basic map
2208 * satisfy the constraint
2209 * adj_eq the given constraint is adjacent (on the outside)
2210 * to an equality of the other basic map
2211 * adj_ineq the given constraint is adjacent (on the outside)
2212 * to an inequality of the other basic map
2214 * We consider seven cases in which we can replace the pair by a single
2215 * basic map. We ignore all "redundant" constraints.
2217 * 1. all constraints of one basic map are valid
2218 * => the other basic map is a subset and can be removed
2220 * 2. all constraints of both basic maps are either "valid" or "cut"
2221 * and the facets corresponding to the "cut" constraints
2222 * of one of the basic maps lies entirely inside the other basic map
2223 * => the pair can be replaced by a basic map consisting
2224 * of the valid constraints in both basic maps
2226 * 3. there is a single pair of adjacent inequalities
2227 * (all other constraints are "valid")
2228 * => the pair can be replaced by a basic map consisting
2229 * of the valid constraints in both basic maps
2231 * 4. one basic map has a single adjacent inequality, while the other
2232 * constraints are "valid". The other basic map has some
2233 * "cut" constraints, but replacing the adjacent inequality by
2234 * its opposite and adding the valid constraints of the other
2235 * basic map results in a subset of the other basic map
2236 * => the pair can be replaced by a basic map consisting
2237 * of the valid constraints in both basic maps
2239 * 5. there is a single adjacent pair of an inequality and an equality,
2240 * the other constraints of the basic map containing the inequality are
2241 * "valid". Moreover, if the inequality the basic map is relaxed
2242 * and then turned into an equality, then resulting facet lies
2243 * entirely inside the other basic map
2244 * => the pair can be replaced by the basic map containing
2245 * the inequality, with the inequality relaxed.
2247 * 6. there is a single inequality adjacent to an equality,
2248 * the other constraints of the basic map containing the inequality are
2249 * "valid". Moreover, the facets corresponding to both
2250 * the inequality and the equality can be wrapped around their
2251 * ridges to include the other basic map
2252 * => the pair can be replaced by a basic map consisting
2253 * of the valid constraints in both basic maps together
2254 * with all wrapping constraints
2256 * 7. one of the basic maps extends beyond the other by at most one.
2257 * Moreover, the facets corresponding to the cut constraints and
2258 * the pieces of the other basic map at offset one from these cut
2259 * constraints can be wrapped around their ridges to include
2260 * the union of the two basic maps
2261 * => the pair can be replaced by a basic map consisting
2262 * of the valid constraints in both basic maps together
2263 * with all wrapping constraints
2265 * 8. the two basic maps live in adjacent hyperplanes. In principle
2266 * such sets can always be combined through wrapping, but we impose
2267 * that there is only one such pair, to avoid overeager coalescing.
2269 * Throughout the computation, we maintain a collection of tableaus
2270 * corresponding to the basic maps. When the basic maps are dropped
2271 * or combined, the tableaus are modified accordingly.
2273 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2274 struct isl_coalesce_info *info)
2276 enum isl_change change = isl_change_none;
2278 set_ineq_status_in(&info[i], info[j].tab);
2279 if (info[i].bmap->n_ineq && !info[i].ineq)
2280 goto error;
2281 if (any_ineq(&info[i], STATUS_ERROR))
2282 goto error;
2283 if (any_ineq(&info[i], STATUS_SEPARATE))
2284 goto done;
2286 set_ineq_status_in(&info[j], info[i].tab);
2287 if (info[j].bmap->n_ineq && !info[j].ineq)
2288 goto error;
2289 if (any_ineq(&info[j], STATUS_ERROR))
2290 goto error;
2291 if (any_ineq(&info[j], STATUS_SEPARATE))
2292 goto done;
2294 set_eq_status_in(&info[i], info[j].tab);
2295 if (info[i].bmap->n_eq && !info[i].eq)
2296 goto error;
2297 if (any_eq(&info[i], STATUS_ERROR))
2298 goto error;
2300 set_eq_status_in(&info[j], info[i].tab);
2301 if (info[j].bmap->n_eq && !info[j].eq)
2302 goto error;
2303 if (any_eq(&info[j], STATUS_ERROR))
2304 goto error;
2306 if (any_eq(&info[i], STATUS_SEPARATE))
2307 return separating_equality(i, j, info);
2308 if (any_eq(&info[j], STATUS_SEPARATE))
2309 return separating_equality(j, i, info);
2311 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2312 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2313 drop(&info[j]);
2314 change = isl_change_drop_second;
2315 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2316 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2317 drop(&info[i]);
2318 change = isl_change_drop_first;
2319 } else if (any_eq(&info[i], STATUS_ADJ_EQ)) {
2320 change = check_eq_adj_eq(i, j, info);
2321 } else if (any_eq(&info[j], STATUS_ADJ_EQ)) {
2322 change = check_eq_adj_eq(j, i, info);
2323 } else if (any_eq(&info[i], STATUS_ADJ_INEQ) ||
2324 any_eq(&info[j], STATUS_ADJ_INEQ)) {
2325 change = check_adj_eq(i, j, info);
2326 } else if (any_ineq(&info[i], STATUS_ADJ_EQ)) {
2327 change = check_ineq_adj_eq(i, j, info);
2328 } else if (any_ineq(&info[j], STATUS_ADJ_EQ)) {
2329 change = check_ineq_adj_eq(j, i, info);
2330 } else if (any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2331 any_ineq(&info[j], STATUS_ADJ_INEQ)) {
2332 change = check_adj_ineq(i, j, info);
2333 } else {
2334 if (!any_eq(&info[i], STATUS_CUT) &&
2335 !any_eq(&info[j], STATUS_CUT))
2336 change = check_facets(i, j, info);
2337 if (change == isl_change_none)
2338 change = check_wrap(i, j, info);
2341 done:
2342 clear_status(&info[i]);
2343 clear_status(&info[j]);
2344 return change;
2345 error:
2346 clear_status(&info[i]);
2347 clear_status(&info[j]);
2348 return isl_change_error;
2351 /* Check if the union of the given pair of basic maps
2352 * can be represented by a single basic map.
2353 * If so, replace the pair by the single basic map and return
2354 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2355 * Otherwise, return isl_change_none.
2356 * The two basic maps are assumed to live in the same local space.
2358 static enum isl_change coalesce_local_pair(int i, int j,
2359 struct isl_coalesce_info *info)
2361 init_status(&info[i]);
2362 init_status(&info[j]);
2363 return coalesce_local_pair_reuse(i, j, info);
2366 /* Shift the integer division at position "div" of the basic map
2367 * represented by "info" by "shift".
2369 * That is, if the integer division has the form
2371 * floor(f(x)/d)
2373 * then replace it by
2375 * floor((f(x) + shift * d)/d) - shift
2377 static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2378 isl_int shift)
2380 unsigned total;
2382 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2383 if (!info->bmap)
2384 return isl_stat_error;
2386 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2387 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2388 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2389 return isl_stat_error;
2391 return isl_stat_ok;
2394 /* If the integer division at position "div" is defined by an equality,
2395 * i.e., a stride constraint, then change the integer division expression
2396 * to have a constant term equal to zero.
2398 * Let the equality constraint be
2400 * c + f + m a = 0
2402 * The integer division expression is then typically of the form
2404 * a = floor((-f - c')/m)
2406 * The integer division is first shifted by t = floor(c/m),
2407 * turning the equality constraint into
2409 * c - m floor(c/m) + f + m a' = 0
2411 * i.e.,
2413 * (c mod m) + f + m a' = 0
2415 * That is,
2417 * a' = (-f - (c mod m))/m = floor((-f)/m)
2419 * because a' is an integer and 0 <= (c mod m) < m.
2420 * The constant term of a' can therefore be zeroed out,
2421 * but only if the integer division expression is of the expected form.
2423 static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2425 isl_bool defined, valid;
2426 isl_stat r;
2427 isl_constraint *c;
2428 isl_int shift, stride;
2430 defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2431 div, &c);
2432 if (defined < 0)
2433 return isl_stat_error;
2434 if (!defined)
2435 return isl_stat_ok;
2436 if (!c)
2437 return isl_stat_error;
2438 valid = isl_constraint_is_div_equality(c, div);
2439 isl_int_init(shift);
2440 isl_int_init(stride);
2441 isl_constraint_get_constant(c, &shift);
2442 isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2443 isl_int_fdiv_q(shift, shift, stride);
2444 r = shift_div(info, div, shift);
2445 isl_int_clear(stride);
2446 isl_int_clear(shift);
2447 isl_constraint_free(c);
2448 if (r < 0 || valid < 0)
2449 return isl_stat_error;
2450 if (!valid)
2451 return isl_stat_ok;
2452 info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2453 info->bmap, div, 0);
2454 if (!info->bmap)
2455 return isl_stat_error;
2456 return isl_stat_ok;
2459 /* The basic maps represented by "info1" and "info2" are known
2460 * to have the same number of integer divisions.
2461 * Check if pairs of integer divisions are equal to each other
2462 * despite the fact that they differ by a rational constant.
2464 * In particular, look for any pair of integer divisions that
2465 * only differ in their constant terms.
2466 * If either of these integer divisions is defined
2467 * by stride constraints, then modify it to have a zero constant term.
2468 * If both are defined by stride constraints then in the end they will have
2469 * the same (zero) constant term.
2471 static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2472 struct isl_coalesce_info *info2)
2474 int i, n;
2476 n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2477 for (i = 0; i < n; ++i) {
2478 isl_bool known, harmonize;
2480 known = isl_basic_map_div_is_known(info1->bmap, i);
2481 if (known >= 0 && known)
2482 known = isl_basic_map_div_is_known(info2->bmap, i);
2483 if (known < 0)
2484 return isl_stat_error;
2485 if (!known)
2486 continue;
2487 harmonize = isl_basic_map_equal_div_expr_except_constant(
2488 info1->bmap, i, info2->bmap, i);
2489 if (harmonize < 0)
2490 return isl_stat_error;
2491 if (!harmonize)
2492 continue;
2493 if (normalize_stride_div(info1, i) < 0)
2494 return isl_stat_error;
2495 if (normalize_stride_div(info2, i) < 0)
2496 return isl_stat_error;
2499 return isl_stat_ok;
2502 /* If "shift" is an integer constant, then shift the integer division
2503 * at position "div" of the basic map represented by "info" by "shift".
2504 * If "shift" is not an integer constant, then do nothing.
2505 * If "shift" is equal to zero, then no shift needs to be performed either.
2507 * That is, if the integer division has the form
2509 * floor(f(x)/d)
2511 * then replace it by
2513 * floor((f(x) + shift * d)/d) - shift
2515 static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2516 __isl_keep isl_aff *shift)
2518 isl_bool cst;
2519 isl_stat r;
2520 isl_int d;
2521 isl_val *c;
2523 cst = isl_aff_is_cst(shift);
2524 if (cst < 0 || !cst)
2525 return cst < 0 ? isl_stat_error : isl_stat_ok;
2527 c = isl_aff_get_constant_val(shift);
2528 cst = isl_val_is_int(c);
2529 if (cst >= 0 && cst)
2530 cst = isl_bool_not(isl_val_is_zero(c));
2531 if (cst < 0 || !cst) {
2532 isl_val_free(c);
2533 return cst < 0 ? isl_stat_error : isl_stat_ok;
2536 isl_int_init(d);
2537 r = isl_val_get_num_isl_int(c, &d);
2538 if (r >= 0)
2539 r = shift_div(info, div, d);
2540 isl_int_clear(d);
2542 isl_val_free(c);
2544 return r;
2547 /* Check if some of the divs in the basic map represented by "info1"
2548 * are shifts of the corresponding divs in the basic map represented
2549 * by "info2", taking into account the equality constraints "eq1" of "info1"
2550 * and "eq2" of "info2". If so, align them with those of "info2".
2551 * "info1" and "info2" are assumed to have the same number
2552 * of integer divisions.
2554 * An integer division is considered to be a shift of another integer
2555 * division if, after simplification with respect to the equality
2556 * constraints of the other basic map, one is equal to the other
2557 * plus a constant.
2559 * In particular, for each pair of integer divisions, if both are known,
2560 * have the same denominator and are not already equal to each other,
2561 * simplify each with respect to the equality constraints
2562 * of the other basic map. If the difference is an integer constant,
2563 * then move this difference outside.
2564 * That is, if, after simplification, one integer division is of the form
2566 * floor((f(x) + c_1)/d)
2568 * while the other is of the form
2570 * floor((f(x) + c_2)/d)
2572 * and n = (c_2 - c_1)/d is an integer, then replace the first
2573 * integer division by
2575 * floor((f_1(x) + c_1 + n * d)/d) - n,
2577 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2578 * after simplification with respect to the equality constraints.
2580 static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2581 struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2582 __isl_keep isl_basic_set *eq2)
2584 int i;
2585 int total;
2586 isl_local_space *ls1, *ls2;
2588 total = isl_basic_map_total_dim(info1->bmap);
2589 ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2590 ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2591 for (i = 0; i < info1->bmap->n_div; ++i) {
2592 isl_stat r;
2593 isl_aff *div1, *div2;
2595 if (!isl_local_space_div_is_known(ls1, i) ||
2596 !isl_local_space_div_is_known(ls2, i))
2597 continue;
2598 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2599 continue;
2600 if (isl_seq_eq(info1->bmap->div[i] + 1,
2601 info2->bmap->div[i] + 1, 1 + total))
2602 continue;
2603 div1 = isl_local_space_get_div(ls1, i);
2604 div2 = isl_local_space_get_div(ls2, i);
2605 div1 = isl_aff_substitute_equalities(div1,
2606 isl_basic_set_copy(eq2));
2607 div2 = isl_aff_substitute_equalities(div2,
2608 isl_basic_set_copy(eq1));
2609 div2 = isl_aff_sub(div2, div1);
2610 r = shift_if_cst_int(info1, i, div2);
2611 isl_aff_free(div2);
2612 if (r < 0)
2613 break;
2615 isl_local_space_free(ls1);
2616 isl_local_space_free(ls2);
2618 if (i < info1->bmap->n_div)
2619 return isl_stat_error;
2620 return isl_stat_ok;
2623 /* Check if some of the divs in the basic map represented by "info1"
2624 * are shifts of the corresponding divs in the basic map represented
2625 * by "info2". If so, align them with those of "info2".
2626 * Only do this if "info1" and "info2" have the same number
2627 * of integer divisions.
2629 * An integer division is considered to be a shift of another integer
2630 * division if, after simplification with respect to the equality
2631 * constraints of the other basic map, one is equal to the other
2632 * plus a constant.
2634 * First check if pairs of integer divisions are equal to each other
2635 * despite the fact that they differ by a rational constant.
2636 * If so, try and arrange for them to have the same constant term.
2638 * Then, extract the equality constraints and continue with
2639 * harmonize_divs_with_hulls.
2641 * If the equality constraints of both basic maps are the same,
2642 * then there is no need to perform any shifting since
2643 * the coefficients of the integer divisions should have been
2644 * reduced in the same way.
2646 static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2647 struct isl_coalesce_info *info2)
2649 isl_bool equal;
2650 isl_basic_map *bmap1, *bmap2;
2651 isl_basic_set *eq1, *eq2;
2652 isl_stat r;
2654 if (!info1->bmap || !info2->bmap)
2655 return isl_stat_error;
2657 if (info1->bmap->n_div != info2->bmap->n_div)
2658 return isl_stat_ok;
2659 if (info1->bmap->n_div == 0)
2660 return isl_stat_ok;
2662 if (harmonize_stride_divs(info1, info2) < 0)
2663 return isl_stat_error;
2665 bmap1 = isl_basic_map_copy(info1->bmap);
2666 bmap2 = isl_basic_map_copy(info2->bmap);
2667 eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2668 eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2669 equal = isl_basic_set_plain_is_equal(eq1, eq2);
2670 if (equal < 0)
2671 r = isl_stat_error;
2672 else if (equal)
2673 r = isl_stat_ok;
2674 else
2675 r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2676 isl_basic_set_free(eq1);
2677 isl_basic_set_free(eq2);
2679 return r;
2682 /* Do the two basic maps live in the same local space, i.e.,
2683 * do they have the same (known) divs?
2684 * If either basic map has any unknown divs, then we can only assume
2685 * that they do not live in the same local space.
2687 static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
2688 __isl_keep isl_basic_map *bmap2)
2690 int i;
2691 isl_bool known;
2692 int total;
2694 if (!bmap1 || !bmap2)
2695 return isl_bool_error;
2696 if (bmap1->n_div != bmap2->n_div)
2697 return isl_bool_false;
2699 if (bmap1->n_div == 0)
2700 return isl_bool_true;
2702 known = isl_basic_map_divs_known(bmap1);
2703 if (known < 0 || !known)
2704 return known;
2705 known = isl_basic_map_divs_known(bmap2);
2706 if (known < 0 || !known)
2707 return known;
2709 total = isl_basic_map_total_dim(bmap1);
2710 for (i = 0; i < bmap1->n_div; ++i)
2711 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2712 return isl_bool_false;
2714 return isl_bool_true;
2717 /* Assuming that "tab" contains the equality constraints and
2718 * the initial inequality constraints of "bmap", copy the remaining
2719 * inequality constraints of "bmap" to "Tab".
2721 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2723 int i, n_ineq;
2725 if (!bmap)
2726 return isl_stat_error;
2728 n_ineq = tab->n_con - tab->n_eq;
2729 for (i = n_ineq; i < bmap->n_ineq; ++i)
2730 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2731 return isl_stat_error;
2733 return isl_stat_ok;
2736 /* Description of an integer division that is added
2737 * during an expansion.
2738 * "pos" is the position of the corresponding variable.
2739 * "cst" indicates whether this integer division has a fixed value.
2740 * "val" contains the fixed value, if the value is fixed.
2742 struct isl_expanded {
2743 int pos;
2744 isl_bool cst;
2745 isl_int val;
2748 /* For each of the "n" integer division variables "expanded",
2749 * if the variable has a fixed value, then add two inequality
2750 * constraints expressing the fixed value.
2751 * Otherwise, add the corresponding div constraints.
2752 * The caller is responsible for removing the div constraints
2753 * that it added for all these "n" integer divisions.
2755 * The div constraints and the pair of inequality constraints
2756 * forcing the fixed value cannot both be added for a given variable
2757 * as the combination may render some of the original constraints redundant.
2758 * These would then be ignored during the coalescing detection,
2759 * while they could remain in the fused result.
2761 * The two added inequality constraints are
2763 * -a + v >= 0
2764 * a - v >= 0
2766 * with "a" the variable and "v" its fixed value.
2767 * The facet corresponding to one of these two constraints is selected
2768 * in the tableau to ensure that the pair of inequality constraints
2769 * is treated as an equality constraint.
2771 * The information in info->ineq is thrown away because it was
2772 * computed in terms of div constraints, while some of those
2773 * have now been replaced by these pairs of inequality constraints.
2775 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2776 int n, struct isl_expanded *expanded)
2778 unsigned o_div;
2779 int i;
2780 isl_vec *ineq;
2782 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2783 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2784 if (!ineq)
2785 return isl_stat_error;
2786 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2788 for (i = 0; i < n; ++i) {
2789 if (!expanded[i].cst) {
2790 info->bmap = isl_basic_map_extend_constraints(
2791 info->bmap, 0, 2);
2792 if (isl_basic_map_add_div_constraints(info->bmap,
2793 expanded[i].pos - o_div) < 0)
2794 break;
2795 } else {
2796 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2797 isl_int_set(ineq->el[0], expanded[i].val);
2798 info->bmap = isl_basic_map_add_ineq(info->bmap,
2799 ineq->el);
2800 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2801 isl_int_neg(ineq->el[0], expanded[i].val);
2802 info->bmap = isl_basic_map_add_ineq(info->bmap,
2803 ineq->el);
2804 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2806 if (copy_ineq(info->tab, info->bmap) < 0)
2807 break;
2808 if (expanded[i].cst &&
2809 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2810 break;
2813 isl_vec_free(ineq);
2815 clear_status(info);
2816 init_status(info);
2818 return i < n ? isl_stat_error : isl_stat_ok;
2821 /* Insert the "n" integer division variables "expanded"
2822 * into info->tab and info->bmap and
2823 * update info->ineq with respect to the redundant constraints
2824 * in the resulting tableau.
2825 * "bmap" contains the result of this insertion in info->bmap,
2826 * while info->bmap is the original version
2827 * of "bmap", i.e., the one that corresponds to the current
2828 * state of info->tab. The number of constraints in info->bmap
2829 * is assumed to be the same as the number of constraints
2830 * in info->tab. This is required to be able to detect
2831 * the extra constraints in "bmap".
2833 * In particular, introduce extra variables corresponding
2834 * to the extra integer divisions and add the div constraints
2835 * that were added to "bmap" after info->tab was created
2836 * from info->bmap.
2837 * Furthermore, check if these extra integer divisions happen
2838 * to attain a fixed integer value in info->tab.
2839 * If so, replace the corresponding div constraints by pairs
2840 * of inequality constraints that fix these
2841 * integer divisions to their single integer values.
2842 * Replace info->bmap by "bmap" to match the changes to info->tab.
2843 * info->ineq was computed without a tableau and therefore
2844 * does not take into account the redundant constraints
2845 * in the tableau. Mark them here.
2846 * There is no need to check the newly added div constraints
2847 * since they cannot be redundant.
2848 * The redundancy check is not performed when constants have been discovered
2849 * since info->ineq is completely thrown away in this case.
2851 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2852 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2854 int i, n_ineq;
2855 unsigned n_eq;
2856 struct isl_tab_undo *snap;
2857 int any;
2859 if (!bmap)
2860 return isl_stat_error;
2861 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2862 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2863 "original tableau does not correspond "
2864 "to original basic map", goto error);
2866 if (isl_tab_extend_vars(info->tab, n) < 0)
2867 goto error;
2868 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2869 goto error;
2871 for (i = 0; i < n; ++i) {
2872 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2873 goto error;
2876 snap = isl_tab_snap(info->tab);
2878 n_ineq = info->tab->n_con - info->tab->n_eq;
2879 if (copy_ineq(info->tab, bmap) < 0)
2880 goto error;
2882 isl_basic_map_free(info->bmap);
2883 info->bmap = bmap;
2885 any = 0;
2886 for (i = 0; i < n; ++i) {
2887 expanded[i].cst = isl_tab_is_constant(info->tab,
2888 expanded[i].pos, &expanded[i].val);
2889 if (expanded[i].cst < 0)
2890 return isl_stat_error;
2891 if (expanded[i].cst)
2892 any = 1;
2895 if (any) {
2896 if (isl_tab_rollback(info->tab, snap) < 0)
2897 return isl_stat_error;
2898 info->bmap = isl_basic_map_cow(info->bmap);
2899 if (isl_basic_map_free_inequality(info->bmap, 2 * n) < 0)
2900 return isl_stat_error;
2902 return fix_constant_divs(info, n, expanded);
2905 n_eq = info->bmap->n_eq;
2906 for (i = 0; i < n_ineq; ++i) {
2907 if (isl_tab_is_redundant(info->tab, n_eq + i))
2908 info->ineq[i] = STATUS_REDUNDANT;
2911 return isl_stat_ok;
2912 error:
2913 isl_basic_map_free(bmap);
2914 return isl_stat_error;
2917 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2918 * in isl_basic_map_expand_divs using the expansion "exp" and
2919 * update info->ineq with respect to the redundant constraints
2920 * in the resulting tableau. info->bmap is the original version
2921 * of "bmap", i.e., the one that corresponds to the current
2922 * state of info->tab. The number of constraints in info->bmap
2923 * is assumed to be the same as the number of constraints
2924 * in info->tab. This is required to be able to detect
2925 * the extra constraints in "bmap".
2927 * Extract the positions where extra local variables are introduced
2928 * from "exp" and call tab_insert_divs.
2930 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2931 __isl_take isl_basic_map *bmap)
2933 isl_ctx *ctx;
2934 struct isl_expanded *expanded;
2935 int i, j, k, n;
2936 int extra_var;
2937 unsigned total, pos, n_div;
2938 isl_stat r;
2940 total = isl_basic_map_dim(bmap, isl_dim_all);
2941 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2942 pos = total - n_div;
2943 extra_var = total - info->tab->n_var;
2944 n = n_div - extra_var;
2946 ctx = isl_basic_map_get_ctx(bmap);
2947 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
2948 if (extra_var && !expanded)
2949 goto error;
2951 i = 0;
2952 k = 0;
2953 for (j = 0; j < n_div; ++j) {
2954 if (i < n && exp[i] == j) {
2955 ++i;
2956 continue;
2958 expanded[k++].pos = pos + j;
2961 for (k = 0; k < extra_var; ++k)
2962 isl_int_init(expanded[k].val);
2964 r = tab_insert_divs(info, extra_var, expanded, bmap);
2966 for (k = 0; k < extra_var; ++k)
2967 isl_int_clear(expanded[k].val);
2968 free(expanded);
2970 return r;
2971 error:
2972 isl_basic_map_free(bmap);
2973 return isl_stat_error;
2976 /* Check if the union of the basic maps represented by info[i] and info[j]
2977 * can be represented by a single basic map,
2978 * after expanding the divs of info[i] to match those of info[j].
2979 * If so, replace the pair by the single basic map and return
2980 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2981 * Otherwise, return isl_change_none.
2983 * The caller has already checked for info[j] being a subset of info[i].
2984 * If some of the divs of info[j] are unknown, then the expanded info[i]
2985 * will not have the corresponding div constraints. The other patterns
2986 * therefore cannot apply. Skip the computation in this case.
2988 * The expansion is performed using the divs "div" and expansion "exp"
2989 * computed by the caller.
2990 * info[i].bmap has already been expanded and the result is passed in
2991 * as "bmap".
2992 * The "eq" and "ineq" fields of info[i] reflect the status of
2993 * the constraints of the expanded "bmap" with respect to info[j].tab.
2994 * However, inequality constraints that are redundant in info[i].tab
2995 * have not yet been marked as such because no tableau was available.
2997 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2998 * updating info[i].ineq with respect to the redundant constraints.
2999 * Then try and coalesce the expanded info[i] with info[j],
3000 * reusing the information in info[i].eq and info[i].ineq.
3001 * If this does not result in any coalescing or if it results in info[j]
3002 * getting dropped (which should not happen in practice, since the case
3003 * of info[j] being a subset of info[i] has already been checked by
3004 * the caller), then revert info[i] to its original state.
3006 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
3007 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
3008 int *exp)
3010 isl_bool known;
3011 isl_basic_map *bmap_i;
3012 struct isl_tab_undo *snap;
3013 enum isl_change change = isl_change_none;
3015 known = isl_basic_map_divs_known(info[j].bmap);
3016 if (known < 0 || !known) {
3017 clear_status(&info[i]);
3018 isl_basic_map_free(bmap);
3019 return known < 0 ? isl_change_error : isl_change_none;
3022 bmap_i = isl_basic_map_copy(info[i].bmap);
3023 snap = isl_tab_snap(info[i].tab);
3024 if (expand_tab(&info[i], exp, bmap) < 0)
3025 change = isl_change_error;
3027 init_status(&info[j]);
3028 if (change == isl_change_none)
3029 change = coalesce_local_pair_reuse(i, j, info);
3030 else
3031 clear_status(&info[i]);
3032 if (change != isl_change_none && change != isl_change_drop_second) {
3033 isl_basic_map_free(bmap_i);
3034 } else {
3035 isl_basic_map_free(info[i].bmap);
3036 info[i].bmap = bmap_i;
3038 if (isl_tab_rollback(info[i].tab, snap) < 0)
3039 change = isl_change_error;
3042 return change;
3045 /* Check if the union of "bmap" and the basic map represented by info[j]
3046 * can be represented by a single basic map,
3047 * after expanding the divs of "bmap" to match those of info[j].
3048 * If so, replace the pair by the single basic map and return
3049 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3050 * Otherwise, return isl_change_none.
3052 * In particular, check if the expanded "bmap" contains the basic map
3053 * represented by the tableau info[j].tab.
3054 * The expansion is performed using the divs "div" and expansion "exp"
3055 * computed by the caller.
3056 * Then we check if all constraints of the expanded "bmap" are valid for
3057 * info[j].tab.
3059 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3060 * In this case, the positions of the constraints of info[i].bmap
3061 * with respect to the basic map represented by info[j] are stored
3062 * in info[i].
3064 * If the expanded "bmap" does not contain the basic map
3065 * represented by the tableau info[j].tab and if "i" is not -1,
3066 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
3067 * as well and check if that results in coalescing.
3069 static enum isl_change coalesce_with_expanded_divs(
3070 __isl_keep isl_basic_map *bmap, int i, int j,
3071 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
3073 enum isl_change change = isl_change_none;
3074 struct isl_coalesce_info info_local, *info_i;
3076 info_i = i >= 0 ? &info[i] : &info_local;
3077 init_status(info_i);
3078 bmap = isl_basic_map_copy(bmap);
3079 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
3080 bmap = isl_basic_map_mark_final(bmap);
3082 if (!bmap)
3083 goto error;
3085 info_local.bmap = bmap;
3086 info_i->eq = eq_status_in(bmap, info[j].tab);
3087 if (bmap->n_eq && !info_i->eq)
3088 goto error;
3089 if (any_eq(info_i, STATUS_ERROR))
3090 goto error;
3091 if (any_eq(info_i, STATUS_SEPARATE))
3092 goto done;
3094 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
3095 if (bmap->n_ineq && !info_i->ineq)
3096 goto error;
3097 if (any_ineq(info_i, STATUS_ERROR))
3098 goto error;
3099 if (any_ineq(info_i, STATUS_SEPARATE))
3100 goto done;
3102 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
3103 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
3104 drop(&info[j]);
3105 change = isl_change_drop_second;
3108 if (change == isl_change_none && i != -1)
3109 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
3111 done:
3112 isl_basic_map_free(bmap);
3113 clear_status(info_i);
3114 return change;
3115 error:
3116 isl_basic_map_free(bmap);
3117 clear_status(info_i);
3118 return isl_change_error;
3121 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3122 * can be represented by a single basic map,
3123 * after aligning the divs of "bmap_i" to match those of info[j].
3124 * If so, replace the pair by the single basic map and return
3125 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3126 * Otherwise, return isl_change_none.
3128 * In particular, check if "bmap_i" contains the basic map represented by
3129 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3130 * Note that this can only succeed if the number of divs of "bmap_i"
3131 * is smaller than (or equal to) the number of divs of info[j].
3133 * We first check if the divs of "bmap_i" are all known and form a subset
3134 * of those of info[j].bmap. If so, we pass control over to
3135 * coalesce_with_expanded_divs.
3137 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3139 static enum isl_change coalesce_after_aligning_divs(
3140 __isl_keep isl_basic_map *bmap_i, int i, int j,
3141 struct isl_coalesce_info *info)
3143 isl_bool known;
3144 isl_mat *div_i, *div_j, *div;
3145 int *exp1 = NULL;
3146 int *exp2 = NULL;
3147 isl_ctx *ctx;
3148 enum isl_change change;
3150 known = isl_basic_map_divs_known(bmap_i);
3151 if (known < 0)
3152 return isl_change_error;
3153 if (!known)
3154 return isl_change_none;
3156 ctx = isl_basic_map_get_ctx(bmap_i);
3158 div_i = isl_basic_map_get_divs(bmap_i);
3159 div_j = isl_basic_map_get_divs(info[j].bmap);
3161 if (!div_i || !div_j)
3162 goto error;
3164 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3165 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3166 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
3167 goto error;
3169 div = isl_merge_divs(div_i, div_j, exp1, exp2);
3170 if (!div)
3171 goto error;
3173 if (div->n_row == div_j->n_row)
3174 change = coalesce_with_expanded_divs(bmap_i,
3175 i, j, info, div, exp1);
3176 else
3177 change = isl_change_none;
3179 isl_mat_free(div);
3181 isl_mat_free(div_i);
3182 isl_mat_free(div_j);
3184 free(exp2);
3185 free(exp1);
3187 return change;
3188 error:
3189 isl_mat_free(div_i);
3190 isl_mat_free(div_j);
3191 free(exp1);
3192 free(exp2);
3193 return isl_change_error;
3196 /* Check if basic map "j" is a subset of basic map "i" after
3197 * exploiting the extra equalities of "j" to simplify the divs of "i".
3198 * If so, remove basic map "j" and return isl_change_drop_second.
3200 * If "j" does not have any equalities or if they are the same
3201 * as those of "i", then we cannot exploit them to simplify the divs.
3202 * Similarly, if there are no divs in "i", then they cannot be simplified.
3203 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3204 * then "j" cannot be a subset of "i".
3206 * Otherwise, we intersect "i" with the affine hull of "j" and then
3207 * check if "j" is a subset of the result after aligning the divs.
3208 * If so, then "j" is definitely a subset of "i" and can be removed.
3209 * Note that if after intersection with the affine hull of "j".
3210 * "i" still has more divs than "j", then there is no way we can
3211 * align the divs of "i" to those of "j".
3213 static enum isl_change coalesce_subset_with_equalities(int i, int j,
3214 struct isl_coalesce_info *info)
3216 isl_basic_map *hull_i, *hull_j, *bmap_i;
3217 int equal, empty;
3218 enum isl_change change;
3220 if (info[j].bmap->n_eq == 0)
3221 return isl_change_none;
3222 if (info[i].bmap->n_div == 0)
3223 return isl_change_none;
3225 hull_i = isl_basic_map_copy(info[i].bmap);
3226 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3227 hull_j = isl_basic_map_copy(info[j].bmap);
3228 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3230 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3231 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3232 empty = isl_basic_map_plain_is_empty(hull_j);
3233 isl_basic_map_free(hull_i);
3235 if (equal < 0 || equal || empty < 0 || empty) {
3236 isl_basic_map_free(hull_j);
3237 if (equal < 0 || empty < 0)
3238 return isl_change_error;
3239 return isl_change_none;
3242 bmap_i = isl_basic_map_copy(info[i].bmap);
3243 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3244 if (!bmap_i)
3245 return isl_change_error;
3247 if (bmap_i->n_div > info[j].bmap->n_div) {
3248 isl_basic_map_free(bmap_i);
3249 return isl_change_none;
3252 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3254 isl_basic_map_free(bmap_i);
3256 return change;
3259 /* Check if the union of and the basic maps represented by info[i] and info[j]
3260 * can be represented by a single basic map, by aligning or equating
3261 * their integer divisions.
3262 * If so, replace the pair by the single basic map and return
3263 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3264 * Otherwise, return isl_change_none.
3266 * Note that we only perform any test if the number of divs is different
3267 * in the two basic maps. In case the number of divs is the same,
3268 * we have already established that the divs are different
3269 * in the two basic maps.
3270 * In particular, if the number of divs of basic map i is smaller than
3271 * the number of divs of basic map j, then we check if j is a subset of i
3272 * and vice versa.
3274 static enum isl_change coalesce_divs(int i, int j,
3275 struct isl_coalesce_info *info)
3277 enum isl_change change = isl_change_none;
3279 if (info[i].bmap->n_div < info[j].bmap->n_div)
3280 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3281 if (change != isl_change_none)
3282 return change;
3284 if (info[j].bmap->n_div < info[i].bmap->n_div)
3285 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3286 if (change != isl_change_none)
3287 return invert_change(change);
3289 change = coalesce_subset_with_equalities(i, j, info);
3290 if (change != isl_change_none)
3291 return change;
3293 change = coalesce_subset_with_equalities(j, i, info);
3294 if (change != isl_change_none)
3295 return invert_change(change);
3297 return isl_change_none;
3300 /* Does "bmap" involve any divs that themselves refer to divs?
3302 static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
3304 int i;
3305 unsigned total;
3306 unsigned n_div;
3308 total = isl_basic_map_dim(bmap, isl_dim_all);
3309 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3310 total -= n_div;
3312 for (i = 0; i < n_div; ++i)
3313 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
3314 n_div) != -1)
3315 return isl_bool_true;
3317 return isl_bool_false;
3320 /* Return a list of affine expressions, one for each integer division
3321 * in "bmap_i". For each integer division that also appears in "bmap_j",
3322 * the affine expression is set to NaN. The number of NaNs in the list
3323 * is equal to the number of integer divisions in "bmap_j".
3324 * For the other integer divisions of "bmap_i", the corresponding
3325 * element in the list is a purely affine expression equal to the integer
3326 * division in "hull".
3327 * If no such list can be constructed, then the number of elements
3328 * in the returned list is smaller than the number of integer divisions
3329 * in "bmap_i".
3331 static __isl_give isl_aff_list *set_up_substitutions(
3332 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3333 __isl_take isl_basic_map *hull)
3335 unsigned n_div_i, n_div_j, total;
3336 isl_ctx *ctx;
3337 isl_local_space *ls;
3338 isl_basic_set *wrap_hull;
3339 isl_aff *aff_nan;
3340 isl_aff_list *list;
3341 int i, j;
3343 if (!hull)
3344 return NULL;
3346 ctx = isl_basic_map_get_ctx(hull);
3348 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3349 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3350 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
3352 ls = isl_basic_map_get_local_space(bmap_i);
3353 ls = isl_local_space_wrap(ls);
3354 wrap_hull = isl_basic_map_wrap(hull);
3356 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3357 list = isl_aff_list_alloc(ctx, n_div_i);
3359 j = 0;
3360 for (i = 0; i < n_div_i; ++i) {
3361 isl_aff *aff;
3363 if (j < n_div_j &&
3364 isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3365 0, 2 + total)) {
3366 ++j;
3367 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3368 continue;
3370 if (n_div_i - i <= n_div_j - j)
3371 break;
3373 aff = isl_local_space_get_div(ls, i);
3374 aff = isl_aff_substitute_equalities(aff,
3375 isl_basic_set_copy(wrap_hull));
3376 aff = isl_aff_floor(aff);
3377 if (!aff)
3378 goto error;
3379 if (isl_aff_dim(aff, isl_dim_div) != 0) {
3380 isl_aff_free(aff);
3381 break;
3384 list = isl_aff_list_add(list, aff);
3387 isl_aff_free(aff_nan);
3388 isl_local_space_free(ls);
3389 isl_basic_set_free(wrap_hull);
3391 return list;
3392 error:
3393 isl_aff_free(aff_nan);
3394 isl_local_space_free(ls);
3395 isl_basic_set_free(wrap_hull);
3396 isl_aff_list_free(list);
3397 return NULL;
3400 /* Add variables to info->bmap and info->tab corresponding to the elements
3401 * in "list" that are not set to NaN.
3402 * "extra_var" is the number of these elements.
3403 * "dim" is the offset in the variables of "tab" where we should
3404 * start considering the elements in "list".
3405 * When this function returns, the total number of variables in "tab"
3406 * is equal to "dim" plus the number of elements in "list".
3408 * The newly added existentially quantified variables are not given
3409 * an explicit representation because the corresponding div constraints
3410 * do not appear in info->bmap. These constraints are not added
3411 * to info->bmap because for internal consistency, they would need to
3412 * be added to info->tab as well, where they could combine with the equality
3413 * that is added later to result in constraints that do not hold
3414 * in the original input.
3416 static isl_stat add_sub_vars(struct isl_coalesce_info *info,
3417 __isl_keep isl_aff_list *list, int dim, int extra_var)
3419 int i, j, n, d;
3420 isl_space *space;
3422 space = isl_basic_map_get_space(info->bmap);
3423 info->bmap = isl_basic_map_cow(info->bmap);
3424 info->bmap = isl_basic_map_extend_space(info->bmap, space,
3425 extra_var, 0, 0);
3426 if (!info->bmap)
3427 return isl_stat_error;
3428 n = isl_aff_list_n_aff(list);
3429 for (i = 0; i < n; ++i) {
3430 int is_nan;
3431 isl_aff *aff;
3433 aff = isl_aff_list_get_aff(list, i);
3434 is_nan = isl_aff_is_nan(aff);
3435 isl_aff_free(aff);
3436 if (is_nan < 0)
3437 return isl_stat_error;
3438 if (is_nan)
3439 continue;
3441 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3442 return isl_stat_error;
3443 d = isl_basic_map_alloc_div(info->bmap);
3444 if (d < 0)
3445 return isl_stat_error;
3446 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3447 for (j = d; j > i; --j)
3448 info->bmap = isl_basic_map_swap_div(info->bmap,
3449 j - 1, j);
3450 if (!info->bmap)
3451 return isl_stat_error;
3454 return isl_stat_ok;
3457 /* For each element in "list" that is not set to NaN, fix the corresponding
3458 * variable in "tab" to the purely affine expression defined by the element.
3459 * "dim" is the offset in the variables of "tab" where we should
3460 * start considering the elements in "list".
3462 * This function assumes that a sufficient number of rows and
3463 * elements in the constraint array are available in the tableau.
3465 static isl_stat add_sub_equalities(struct isl_tab *tab,
3466 __isl_keep isl_aff_list *list, int dim)
3468 int i, n;
3469 isl_ctx *ctx;
3470 isl_vec *sub;
3471 isl_aff *aff;
3473 n = isl_aff_list_n_aff(list);
3475 ctx = isl_tab_get_ctx(tab);
3476 sub = isl_vec_alloc(ctx, 1 + dim + n);
3477 if (!sub)
3478 return isl_stat_error;
3479 isl_seq_clr(sub->el + 1 + dim, n);
3481 for (i = 0; i < n; ++i) {
3482 aff = isl_aff_list_get_aff(list, i);
3483 if (!aff)
3484 goto error;
3485 if (isl_aff_is_nan(aff)) {
3486 isl_aff_free(aff);
3487 continue;
3489 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3490 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3491 if (isl_tab_add_eq(tab, sub->el) < 0)
3492 goto error;
3493 isl_int_set_si(sub->el[1 + dim + i], 0);
3494 isl_aff_free(aff);
3497 isl_vec_free(sub);
3498 return isl_stat_ok;
3499 error:
3500 isl_aff_free(aff);
3501 isl_vec_free(sub);
3502 return isl_stat_error;
3505 /* Add variables to info->tab and info->bmap corresponding to the elements
3506 * in "list" that are not set to NaN. The value of the added variable
3507 * in info->tab is fixed to the purely affine expression defined by the element.
3508 * "dim" is the offset in the variables of info->tab where we should
3509 * start considering the elements in "list".
3510 * When this function returns, the total number of variables in info->tab
3511 * is equal to "dim" plus the number of elements in "list".
3513 static isl_stat add_subs(struct isl_coalesce_info *info,
3514 __isl_keep isl_aff_list *list, int dim)
3516 int extra_var;
3517 int n;
3519 if (!list)
3520 return isl_stat_error;
3522 n = isl_aff_list_n_aff(list);
3523 extra_var = n - (info->tab->n_var - dim);
3525 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3526 return isl_stat_error;
3527 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3528 return isl_stat_error;
3529 if (add_sub_vars(info, list, dim, extra_var) < 0)
3530 return isl_stat_error;
3532 return add_sub_equalities(info->tab, list, dim);
3535 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3536 * divisions in "i" but not in "j" to basic map "j", with values
3537 * specified by "list". The total number of elements in "list"
3538 * is equal to the number of integer divisions in "i", while the number
3539 * of NaN elements in the list is equal to the number of integer divisions
3540 * in "j".
3542 * If no coalescing can be performed, then we need to revert basic map "j"
3543 * to its original state. We do the same if basic map "i" gets dropped
3544 * during the coalescing, even though this should not happen in practice
3545 * since we have already checked for "j" being a subset of "i"
3546 * before we reach this stage.
3548 static enum isl_change coalesce_with_subs(int i, int j,
3549 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3551 isl_basic_map *bmap_j;
3552 struct isl_tab_undo *snap;
3553 unsigned dim;
3554 enum isl_change change;
3556 bmap_j = isl_basic_map_copy(info[j].bmap);
3557 snap = isl_tab_snap(info[j].tab);
3559 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3560 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
3561 if (add_subs(&info[j], list, dim) < 0)
3562 goto error;
3564 change = coalesce_local_pair(i, j, info);
3565 if (change != isl_change_none && change != isl_change_drop_first) {
3566 isl_basic_map_free(bmap_j);
3567 } else {
3568 isl_basic_map_free(info[j].bmap);
3569 info[j].bmap = bmap_j;
3571 if (isl_tab_rollback(info[j].tab, snap) < 0)
3572 return isl_change_error;
3575 return change;
3576 error:
3577 isl_basic_map_free(bmap_j);
3578 return isl_change_error;
3581 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3582 * those extra integer divisions in "i" that can be simplified away
3583 * using the extra equalities in "j".
3584 * All divs are assumed to be known and not contain any nested divs.
3586 * We first check if there are any extra equalities in "j" that we
3587 * can exploit. Then we check if every integer division in "i"
3588 * either already appears in "j" or can be simplified using the
3589 * extra equalities to a purely affine expression.
3590 * If these tests succeed, then we try to coalesce the two basic maps
3591 * by introducing extra dimensions in "j" corresponding to
3592 * the extra integer divsisions "i" fixed to the corresponding
3593 * purely affine expression.
3595 static enum isl_change check_coalesce_into_eq(int i, int j,
3596 struct isl_coalesce_info *info)
3598 unsigned n_div_i, n_div_j;
3599 isl_basic_map *hull_i, *hull_j;
3600 isl_bool equal, empty;
3601 isl_aff_list *list;
3602 enum isl_change change;
3604 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3605 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3606 if (n_div_i <= n_div_j)
3607 return isl_change_none;
3608 if (info[j].bmap->n_eq == 0)
3609 return isl_change_none;
3611 hull_i = isl_basic_map_copy(info[i].bmap);
3612 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3613 hull_j = isl_basic_map_copy(info[j].bmap);
3614 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3616 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3617 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3618 empty = isl_basic_map_plain_is_empty(hull_j);
3619 isl_basic_map_free(hull_i);
3621 if (equal < 0 || empty < 0)
3622 goto error;
3623 if (equal || empty) {
3624 isl_basic_map_free(hull_j);
3625 return isl_change_none;
3628 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3629 if (!list)
3630 return isl_change_error;
3631 if (isl_aff_list_n_aff(list) < n_div_i)
3632 change = isl_change_none;
3633 else
3634 change = coalesce_with_subs(i, j, info, list);
3636 isl_aff_list_free(list);
3638 return change;
3639 error:
3640 isl_basic_map_free(hull_j);
3641 return isl_change_error;
3644 /* Check if we can coalesce basic maps "i" and "j" after copying
3645 * those extra integer divisions in one of the basic maps that can
3646 * be simplified away using the extra equalities in the other basic map.
3647 * We require all divs to be known in both basic maps.
3648 * Furthermore, to simplify the comparison of div expressions,
3649 * we do not allow any nested integer divisions.
3651 static enum isl_change check_coalesce_eq(int i, int j,
3652 struct isl_coalesce_info *info)
3654 isl_bool known, nested;
3655 enum isl_change change;
3657 known = isl_basic_map_divs_known(info[i].bmap);
3658 if (known < 0 || !known)
3659 return known < 0 ? isl_change_error : isl_change_none;
3660 known = isl_basic_map_divs_known(info[j].bmap);
3661 if (known < 0 || !known)
3662 return known < 0 ? isl_change_error : isl_change_none;
3663 nested = has_nested_div(info[i].bmap);
3664 if (nested < 0 || nested)
3665 return nested < 0 ? isl_change_error : isl_change_none;
3666 nested = has_nested_div(info[j].bmap);
3667 if (nested < 0 || nested)
3668 return nested < 0 ? isl_change_error : isl_change_none;
3670 change = check_coalesce_into_eq(i, j, info);
3671 if (change != isl_change_none)
3672 return change;
3673 change = check_coalesce_into_eq(j, i, info);
3674 if (change != isl_change_none)
3675 return invert_change(change);
3677 return isl_change_none;
3680 /* Check if the union of the given pair of basic maps
3681 * can be represented by a single basic map.
3682 * If so, replace the pair by the single basic map and return
3683 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3684 * Otherwise, return isl_change_none.
3686 * We first check if the two basic maps live in the same local space,
3687 * after aligning the divs that differ by only an integer constant.
3688 * If so, we do the complete check. Otherwise, we check if they have
3689 * the same number of integer divisions and can be coalesced, if one is
3690 * an obvious subset of the other or if the extra integer divisions
3691 * of one basic map can be simplified away using the extra equalities
3692 * of the other basic map.
3694 * Note that trying to coalesce pairs of disjuncts with the same
3695 * number, but different local variables may drop the explicit
3696 * representation of some of these local variables.
3697 * This operation is therefore not performed when
3698 * the "coalesce_preserve_locals" option is set.
3700 static enum isl_change coalesce_pair(int i, int j,
3701 struct isl_coalesce_info *info)
3703 int preserve;
3704 isl_bool same;
3705 enum isl_change change;
3706 isl_ctx *ctx;
3708 if (harmonize_divs(&info[i], &info[j]) < 0)
3709 return isl_change_error;
3710 same = same_divs(info[i].bmap, info[j].bmap);
3711 if (same < 0)
3712 return isl_change_error;
3713 if (same)
3714 return coalesce_local_pair(i, j, info);
3716 ctx = isl_basic_map_get_ctx(info[i].bmap);
3717 preserve = isl_options_get_coalesce_preserve_locals(ctx);
3718 if (!preserve && info[i].bmap->n_div == info[j].bmap->n_div) {
3719 change = coalesce_local_pair(i, j, info);
3720 if (change != isl_change_none)
3721 return change;
3724 change = coalesce_divs(i, j, info);
3725 if (change != isl_change_none)
3726 return change;
3728 return check_coalesce_eq(i, j, info);
3731 /* Return the maximum of "a" and "b".
3733 static int isl_max(int a, int b)
3735 return a > b ? a : b;
3738 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3739 * with those in the range [start2, end2[, skipping basic maps
3740 * that have been removed (either before or within this function).
3742 * For each basic map i in the first range, we check if it can be coalesced
3743 * with respect to any previously considered basic map j in the second range.
3744 * If i gets dropped (because it was a subset of some j), then
3745 * we can move on to the next basic map.
3746 * If j gets dropped, we need to continue checking against the other
3747 * previously considered basic maps.
3748 * If the two basic maps got fused, then we recheck the fused basic map
3749 * against the previously considered basic maps, starting at i + 1
3750 * (even if start2 is greater than i + 1).
3752 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3753 int start1, int end1, int start2, int end2)
3755 int i, j;
3757 for (i = end1 - 1; i >= start1; --i) {
3758 if (info[i].removed)
3759 continue;
3760 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3761 enum isl_change changed;
3763 if (info[j].removed)
3764 continue;
3765 if (info[i].removed)
3766 isl_die(ctx, isl_error_internal,
3767 "basic map unexpectedly removed",
3768 return -1);
3769 changed = coalesce_pair(i, j, info);
3770 switch (changed) {
3771 case isl_change_error:
3772 return -1;
3773 case isl_change_none:
3774 case isl_change_drop_second:
3775 continue;
3776 case isl_change_drop_first:
3777 j = end2;
3778 break;
3779 case isl_change_fuse:
3780 j = i;
3781 break;
3786 return 0;
3789 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3791 * We consider groups of basic maps that live in the same apparent
3792 * affine hull and we first coalesce within such a group before we
3793 * coalesce the elements in the group with elements of previously
3794 * considered groups. If a fuse happens during the second phase,
3795 * then we also reconsider the elements within the group.
3797 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3799 int start, end;
3801 for (end = n; end > 0; end = start) {
3802 start = end - 1;
3803 while (start >= 1 &&
3804 info[start - 1].hull_hash == info[start].hull_hash)
3805 start--;
3806 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3807 return -1;
3808 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3809 return -1;
3812 return 0;
3815 /* Update the basic maps in "map" based on the information in "info".
3816 * In particular, remove the basic maps that have been marked removed and
3817 * update the others based on the information in the corresponding tableau.
3818 * Since we detected implicit equalities without calling
3819 * isl_basic_map_gauss, we need to do it now.
3820 * Also call isl_basic_map_simplify if we may have lost the definition
3821 * of one or more integer divisions.
3823 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3824 int n, struct isl_coalesce_info *info)
3826 int i;
3828 if (!map)
3829 return NULL;
3831 for (i = n - 1; i >= 0; --i) {
3832 if (info[i].removed) {
3833 isl_basic_map_free(map->p[i]);
3834 if (i != map->n - 1)
3835 map->p[i] = map->p[map->n - 1];
3836 map->n--;
3837 continue;
3840 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3841 info[i].tab);
3842 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3843 if (info[i].simplify)
3844 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3845 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3846 if (!info[i].bmap)
3847 return isl_map_free(map);
3848 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3849 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3850 isl_basic_map_free(map->p[i]);
3851 map->p[i] = info[i].bmap;
3852 info[i].bmap = NULL;
3855 return map;
3858 /* For each pair of basic maps in the map, check if the union of the two
3859 * can be represented by a single basic map.
3860 * If so, replace the pair by the single basic map and start over.
3862 * We factor out any (hidden) common factor from the constraint
3863 * coefficients to improve the detection of adjacent constraints.
3865 * Since we are constructing the tableaus of the basic maps anyway,
3866 * we exploit them to detect implicit equalities and redundant constraints.
3867 * This also helps the coalescing as it can ignore the redundant constraints.
3868 * In order to avoid confusion, we make all implicit equalities explicit
3869 * in the basic maps. We don't call isl_basic_map_gauss, though,
3870 * as that may affect the number of constraints.
3871 * This means that we have to call isl_basic_map_gauss at the end
3872 * of the computation (in update_basic_maps and in drop) to ensure that
3873 * the basic maps are not left in an unexpected state.
3874 * For each basic map, we also compute the hash of the apparent affine hull
3875 * for use in coalesce.
3877 __isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
3879 int i;
3880 unsigned n;
3881 isl_ctx *ctx;
3882 struct isl_coalesce_info *info = NULL;
3884 map = isl_map_remove_empty_parts(map);
3885 if (!map)
3886 return NULL;
3888 if (map->n <= 1)
3889 return map;
3891 ctx = isl_map_get_ctx(map);
3892 map = isl_map_sort_divs(map);
3893 map = isl_map_cow(map);
3895 if (!map)
3896 return NULL;
3898 n = map->n;
3900 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3901 if (!info)
3902 goto error;
3904 for (i = 0; i < map->n; ++i) {
3905 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3906 if (!map->p[i])
3907 goto error;
3908 info[i].bmap = isl_basic_map_copy(map->p[i]);
3909 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3910 if (!info[i].tab)
3911 goto error;
3912 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3913 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3914 goto error;
3915 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3916 info[i].bmap);
3917 if (!info[i].bmap)
3918 goto error;
3919 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3920 if (isl_tab_detect_redundant(info[i].tab) < 0)
3921 goto error;
3922 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3923 goto error;
3925 for (i = map->n - 1; i >= 0; --i)
3926 if (info[i].tab->empty)
3927 drop(&info[i]);
3929 if (coalesce(ctx, n, info) < 0)
3930 goto error;
3932 map = update_basic_maps(map, n, info);
3934 clear_coalesce_info(n, info);
3936 return map;
3937 error:
3938 clear_coalesce_info(n, info);
3939 isl_map_free(map);
3940 return NULL;
3943 /* For each pair of basic sets in the set, check if the union of the two
3944 * can be represented by a single basic set.
3945 * If so, replace the pair by the single basic set and start over.
3947 struct isl_set *isl_set_coalesce(struct isl_set *set)
3949 return set_from_map(isl_map_coalesce(set_to_map(set)));