isl_*_mod_multi_val: plug memory leak on error path
[isl.git] / isl_coalesce.c
blobf516f694c03527098b40ff0ded241c0bff83592a
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_vec_private.h>
29 #include <isl_aff_private.h>
30 #include <isl_equalities.h>
32 #include <set_to_map.c>
33 #include <set_from_map.c>
35 #define STATUS_ERROR -1
36 #define STATUS_REDUNDANT 1
37 #define STATUS_VALID 2
38 #define STATUS_SEPARATE 3
39 #define STATUS_CUT 4
40 #define STATUS_ADJ_EQ 5
41 #define STATUS_ADJ_INEQ 6
43 static int status_in(isl_int *ineq, struct isl_tab *tab)
45 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
46 switch (type) {
47 default:
48 case isl_ineq_error: return STATUS_ERROR;
49 case isl_ineq_redundant: return STATUS_VALID;
50 case isl_ineq_separate: return STATUS_SEPARATE;
51 case isl_ineq_cut: return STATUS_CUT;
52 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
53 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
57 /* Compute the position of the equalities of basic map "bmap_i"
58 * with respect to the basic map represented by "tab_j".
59 * The resulting array has twice as many entries as the number
60 * of equalities corresponding to the two inequalties to which
61 * each equality corresponds.
63 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
64 struct isl_tab *tab_j)
66 int k, l;
67 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
68 unsigned dim;
70 if (!eq)
71 return NULL;
73 dim = isl_basic_map_total_dim(bmap_i);
74 for (k = 0; k < bmap_i->n_eq; ++k) {
75 for (l = 0; l < 2; ++l) {
76 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
77 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
78 if (eq[2 * k + l] == STATUS_ERROR)
79 goto error;
81 if (eq[2 * k] == STATUS_SEPARATE ||
82 eq[2 * k + 1] == STATUS_SEPARATE)
83 break;
86 return eq;
87 error:
88 free(eq);
89 return NULL;
92 /* Compute the position of the inequalities of basic map "bmap_i"
93 * (also represented by "tab_i", if not NULL) with respect to the basic map
94 * represented by "tab_j".
96 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
97 struct isl_tab *tab_i, struct isl_tab *tab_j)
99 int k;
100 unsigned n_eq = bmap_i->n_eq;
101 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
103 if (!ineq)
104 return NULL;
106 for (k = 0; k < bmap_i->n_ineq; ++k) {
107 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
108 ineq[k] = STATUS_REDUNDANT;
109 continue;
111 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
112 if (ineq[k] == STATUS_ERROR)
113 goto error;
114 if (ineq[k] == STATUS_SEPARATE)
115 break;
118 return ineq;
119 error:
120 free(ineq);
121 return NULL;
124 static int any(int *con, unsigned len, int status)
126 int i;
128 for (i = 0; i < len ; ++i)
129 if (con[i] == status)
130 return 1;
131 return 0;
134 static int count(int *con, unsigned len, int status)
136 int i;
137 int c = 0;
139 for (i = 0; i < len ; ++i)
140 if (con[i] == status)
141 c++;
142 return c;
145 static int all(int *con, unsigned len, int status)
147 int i;
149 for (i = 0; i < len ; ++i) {
150 if (con[i] == STATUS_REDUNDANT)
151 continue;
152 if (con[i] != status)
153 return 0;
155 return 1;
158 /* Internal information associated to a basic map in a map
159 * that is to be coalesced by isl_map_coalesce.
161 * "bmap" is the basic map itself (or NULL if "removed" is set)
162 * "tab" is the corresponding tableau (or NULL if "removed" is set)
163 * "hull_hash" identifies the affine space in which "bmap" lives.
164 * "removed" is set if this basic map has been removed from the map
165 * "simplify" is set if this basic map may have some unknown integer
166 * divisions that were not present in the input basic maps. The basic
167 * map should then be simplified such that we may be able to find
168 * a definition among the constraints.
170 * "eq" and "ineq" are only set if we are currently trying to coalesce
171 * this basic map with another basic map, in which case they represent
172 * the position of the inequalities of this basic map with respect to
173 * the other basic map. The number of elements in the "eq" array
174 * is twice the number of equalities in the "bmap", corresponding
175 * to the two inequalities that make up each equality.
177 struct isl_coalesce_info {
178 isl_basic_map *bmap;
179 struct isl_tab *tab;
180 uint32_t hull_hash;
181 int removed;
182 int simplify;
183 int *eq;
184 int *ineq;
187 /* Are all non-redundant constraints of the basic map represented by "info"
188 * either valid or cut constraints with respect to the other basic map?
190 static int all_valid_or_cut(struct isl_coalesce_info *info)
192 int i;
194 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
195 if (info->eq[i] == STATUS_REDUNDANT)
196 continue;
197 if (info->eq[i] == STATUS_VALID)
198 continue;
199 if (info->eq[i] == STATUS_CUT)
200 continue;
201 return 0;
204 for (i = 0; i < info->bmap->n_ineq; ++i) {
205 if (info->ineq[i] == STATUS_REDUNDANT)
206 continue;
207 if (info->ineq[i] == STATUS_VALID)
208 continue;
209 if (info->ineq[i] == STATUS_CUT)
210 continue;
211 return 0;
214 return 1;
217 /* Compute the hash of the (apparent) affine hull of info->bmap (with
218 * the existentially quantified variables removed) and store it
219 * in info->hash.
221 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
223 isl_basic_map *hull;
224 unsigned n_div;
226 hull = isl_basic_map_copy(info->bmap);
227 hull = isl_basic_map_plain_affine_hull(hull);
228 n_div = isl_basic_map_dim(hull, isl_dim_div);
229 hull = isl_basic_map_drop_constraints_involving_dims(hull,
230 isl_dim_div, 0, n_div);
231 info->hull_hash = isl_basic_map_get_hash(hull);
232 isl_basic_map_free(hull);
234 return hull ? 0 : -1;
237 /* Free all the allocated memory in an array
238 * of "n" isl_coalesce_info elements.
240 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
242 int i;
244 if (!info)
245 return;
247 for (i = 0; i < n; ++i) {
248 isl_basic_map_free(info[i].bmap);
249 isl_tab_free(info[i].tab);
252 free(info);
255 /* Drop the basic map represented by "info".
256 * That is, clear the memory associated to the entry and
257 * mark it as having been removed.
259 static void drop(struct isl_coalesce_info *info)
261 info->bmap = isl_basic_map_free(info->bmap);
262 isl_tab_free(info->tab);
263 info->tab = NULL;
264 info->removed = 1;
267 /* Exchange the information in "info1" with that in "info2".
269 static void exchange(struct isl_coalesce_info *info1,
270 struct isl_coalesce_info *info2)
272 struct isl_coalesce_info info;
274 info = *info1;
275 *info1 = *info2;
276 *info2 = info;
279 /* This type represents the kind of change that has been performed
280 * while trying to coalesce two basic maps.
282 * isl_change_none: nothing was changed
283 * isl_change_drop_first: the first basic map was removed
284 * isl_change_drop_second: the second basic map was removed
285 * isl_change_fuse: the two basic maps were replaced by a new basic map.
287 enum isl_change {
288 isl_change_error = -1,
289 isl_change_none = 0,
290 isl_change_drop_first,
291 isl_change_drop_second,
292 isl_change_fuse,
295 /* Update "change" based on an interchange of the first and the second
296 * basic map. That is, interchange isl_change_drop_first and
297 * isl_change_drop_second.
299 static enum isl_change invert_change(enum isl_change change)
301 switch (change) {
302 case isl_change_error:
303 return isl_change_error;
304 case isl_change_none:
305 return isl_change_none;
306 case isl_change_drop_first:
307 return isl_change_drop_second;
308 case isl_change_drop_second:
309 return isl_change_drop_first;
310 case isl_change_fuse:
311 return isl_change_fuse;
314 return isl_change_error;
317 /* Add the valid constraints of the basic map represented by "info"
318 * to "bmap". "len" is the size of the constraints.
319 * If only one of the pair of inequalities that make up an equality
320 * is valid, then add that inequality.
322 static __isl_give isl_basic_map *add_valid_constraints(
323 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
324 unsigned len)
326 int k, l;
328 if (!bmap)
329 return NULL;
331 for (k = 0; k < info->bmap->n_eq; ++k) {
332 if (info->eq[2 * k] == STATUS_VALID &&
333 info->eq[2 * k + 1] == STATUS_VALID) {
334 l = isl_basic_map_alloc_equality(bmap);
335 if (l < 0)
336 return isl_basic_map_free(bmap);
337 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
338 } else if (info->eq[2 * k] == STATUS_VALID) {
339 l = isl_basic_map_alloc_inequality(bmap);
340 if (l < 0)
341 return isl_basic_map_free(bmap);
342 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
343 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
344 l = isl_basic_map_alloc_inequality(bmap);
345 if (l < 0)
346 return isl_basic_map_free(bmap);
347 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
351 for (k = 0; k < info->bmap->n_ineq; ++k) {
352 if (info->ineq[k] != STATUS_VALID)
353 continue;
354 l = isl_basic_map_alloc_inequality(bmap);
355 if (l < 0)
356 return isl_basic_map_free(bmap);
357 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
360 return bmap;
363 /* Is "bmap" defined by a number of (non-redundant) constraints that
364 * is greater than the number of constraints of basic maps i and j combined?
365 * Equalities are counted as two inequalities.
367 static int number_of_constraints_increases(int i, int j,
368 struct isl_coalesce_info *info,
369 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
371 int k, n_old, n_new;
373 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
374 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
376 n_new = 2 * bmap->n_eq;
377 for (k = 0; k < bmap->n_ineq; ++k)
378 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
379 ++n_new;
381 return n_new > n_old;
384 /* Replace the pair of basic maps i and j by the basic map bounded
385 * by the valid constraints in both basic maps and the constraints
386 * in extra (if not NULL).
387 * Place the fused basic map in the position that is the smallest of i and j.
389 * If "detect_equalities" is set, then look for equalities encoded
390 * as pairs of inequalities.
391 * If "check_number" is set, then the original basic maps are only
392 * replaced if the total number of constraints does not increase.
393 * While the number of integer divisions in the two basic maps
394 * is assumed to be the same, the actual definitions may be different.
395 * We only copy the definition from one of the basic map if it is
396 * the same as that of the other basic map. Otherwise, we mark
397 * the integer division as unknown and simplify the basic map
398 * in an attempt to recover the integer division definition.
400 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
401 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
403 int k, l;
404 struct isl_basic_map *fused = NULL;
405 struct isl_tab *fused_tab = NULL;
406 unsigned total = isl_basic_map_total_dim(info[i].bmap);
407 unsigned extra_rows = extra ? extra->n_row : 0;
408 unsigned n_eq, n_ineq;
409 int simplify = 0;
411 if (j < i)
412 return fuse(j, i, info, extra, detect_equalities, check_number);
414 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
415 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
416 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
417 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
418 fused = add_valid_constraints(fused, &info[i], 1 + total);
419 fused = add_valid_constraints(fused, &info[j], 1 + total);
420 if (!fused)
421 goto error;
422 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
423 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
424 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
426 for (k = 0; k < info[i].bmap->n_div; ++k) {
427 int l = isl_basic_map_alloc_div(fused);
428 if (l < 0)
429 goto error;
430 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
431 1 + 1 + total)) {
432 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
433 1 + 1 + total);
434 } else {
435 isl_int_set_si(fused->div[l][0], 0);
436 simplify = 1;
440 for (k = 0; k < extra_rows; ++k) {
441 l = isl_basic_map_alloc_inequality(fused);
442 if (l < 0)
443 goto error;
444 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
447 if (detect_equalities)
448 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
449 fused = isl_basic_map_gauss(fused, NULL);
450 if (simplify || info[j].simplify) {
451 fused = isl_basic_map_simplify(fused);
452 info[i].simplify = 0;
454 fused = isl_basic_map_finalize(fused);
456 fused_tab = isl_tab_from_basic_map(fused, 0);
457 if (isl_tab_detect_redundant(fused_tab) < 0)
458 goto error;
460 if (check_number &&
461 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
462 isl_tab_free(fused_tab);
463 isl_basic_map_free(fused);
464 return isl_change_none;
467 isl_basic_map_free(info[i].bmap);
468 info[i].bmap = fused;
469 isl_tab_free(info[i].tab);
470 info[i].tab = fused_tab;
471 drop(&info[j]);
473 return isl_change_fuse;
474 error:
475 isl_tab_free(fused_tab);
476 isl_basic_map_free(fused);
477 return isl_change_error;
480 /* Given a pair of basic maps i and j such that all constraints are either
481 * "valid" or "cut", check if the facets corresponding to the "cut"
482 * constraints of i lie entirely within basic map j.
483 * If so, replace the pair by the basic map consisting of the valid
484 * constraints in both basic maps.
485 * Checking whether the facet lies entirely within basic map j
486 * is performed by checking whether the constraints of basic map j
487 * are valid for the facet. These tests are performed on a rational
488 * tableau to avoid the theoretical possibility that a constraint
489 * that was considered to be a cut constraint for the entire basic map i
490 * happens to be considered to be a valid constraint for the facet,
491 * even though it cuts off the same rational points.
493 * To see that we are not introducing any extra points, call the
494 * two basic maps A and B and the resulting map U and let x
495 * be an element of U \setminus ( A \cup B ).
496 * A line connecting x with an element of A \cup B meets a facet F
497 * of either A or B. Assume it is a facet of B and let c_1 be
498 * the corresponding facet constraint. We have c_1(x) < 0 and
499 * so c_1 is a cut constraint. This implies that there is some
500 * (possibly rational) point x' satisfying the constraints of A
501 * and the opposite of c_1 as otherwise c_1 would have been marked
502 * valid for A. The line connecting x and x' meets a facet of A
503 * in a (possibly rational) point that also violates c_1, but this
504 * is impossible since all cut constraints of B are valid for all
505 * cut facets of A.
506 * In case F is a facet of A rather than B, then we can apply the
507 * above reasoning to find a facet of B separating x from A \cup B first.
509 static enum isl_change check_facets(int i, int j,
510 struct isl_coalesce_info *info)
512 int k, l;
513 struct isl_tab_undo *snap, *snap2;
514 unsigned n_eq = info[i].bmap->n_eq;
516 snap = isl_tab_snap(info[i].tab);
517 if (isl_tab_mark_rational(info[i].tab) < 0)
518 return isl_change_error;
519 snap2 = isl_tab_snap(info[i].tab);
521 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
522 if (info[i].ineq[k] != STATUS_CUT)
523 continue;
524 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
525 return isl_change_error;
526 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
527 int stat;
528 if (info[j].ineq[l] != STATUS_CUT)
529 continue;
530 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
531 if (stat < 0)
532 return isl_change_error;
533 if (stat != STATUS_VALID)
534 break;
536 if (isl_tab_rollback(info[i].tab, snap2) < 0)
537 return isl_change_error;
538 if (l < info[j].bmap->n_ineq)
539 break;
542 if (k < info[i].bmap->n_ineq) {
543 if (isl_tab_rollback(info[i].tab, snap) < 0)
544 return isl_change_error;
545 return isl_change_none;
547 return fuse(i, j, info, NULL, 0, 0);
550 /* Check if info->bmap contains the basic map represented
551 * by the tableau "tab".
552 * For each equality, we check both the constraint itself
553 * (as an inequality) and its negation. Make sure the
554 * equality is returned to its original state before returning.
556 static int contains(struct isl_coalesce_info *info, struct isl_tab *tab)
558 int k;
559 unsigned dim;
560 isl_basic_map *bmap = info->bmap;
562 dim = isl_basic_map_total_dim(bmap);
563 for (k = 0; k < bmap->n_eq; ++k) {
564 int stat;
565 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
566 stat = status_in(bmap->eq[k], tab);
567 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
568 if (stat < 0)
569 return -1;
570 if (stat != STATUS_VALID)
571 return 0;
572 stat = status_in(bmap->eq[k], tab);
573 if (stat < 0)
574 return -1;
575 if (stat != STATUS_VALID)
576 return 0;
579 for (k = 0; k < bmap->n_ineq; ++k) {
580 int stat;
581 if (info->ineq[k] == STATUS_REDUNDANT)
582 continue;
583 stat = status_in(bmap->ineq[k], tab);
584 if (stat < 0)
585 return -1;
586 if (stat != STATUS_VALID)
587 return 0;
589 return 1;
592 /* Basic map "i" has an inequality (say "k") that is adjacent
593 * to some inequality of basic map "j". All the other inequalities
594 * are valid for "j".
595 * Check if basic map "j" forms an extension of basic map "i".
597 * Note that this function is only called if some of the equalities or
598 * inequalities of basic map "j" do cut basic map "i". The function is
599 * correct even if there are no such cut constraints, but in that case
600 * the additional checks performed by this function are overkill.
602 * In particular, we replace constraint k, say f >= 0, by constraint
603 * f <= -1, add the inequalities of "j" that are valid for "i"
604 * and check if the result is a subset of basic map "j".
605 * If so, then we know that this result is exactly equal to basic map "j"
606 * since all its constraints are valid for basic map "j".
607 * By combining the valid constraints of "i" (all equalities and all
608 * inequalities except "k") and the valid constraints of "j" we therefore
609 * obtain a basic map that is equal to their union.
610 * In this case, there is no need to perform a rollback of the tableau
611 * since it is going to be destroyed in fuse().
614 * |\__ |\__
615 * | \__ | \__
616 * | \_ => | \__
617 * |_______| _ |_________\
620 * |\ |\
621 * | \ | \
622 * | \ | \
623 * | | | \
624 * | ||\ => | \
625 * | || \ | \
626 * | || | | |
627 * |__||_/ |_____/
629 static enum isl_change is_adj_ineq_extension(int i, int j,
630 struct isl_coalesce_info *info)
632 int k;
633 struct isl_tab_undo *snap;
634 unsigned n_eq = info[i].bmap->n_eq;
635 unsigned total = isl_basic_map_total_dim(info[i].bmap);
636 int r;
637 int super;
639 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
640 return isl_change_error;
642 for (k = 0; k < info[i].bmap->n_ineq; ++k)
643 if (info[i].ineq[k] == STATUS_ADJ_INEQ)
644 break;
645 if (k >= info[i].bmap->n_ineq)
646 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
647 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
648 return isl_change_error);
650 snap = isl_tab_snap(info[i].tab);
652 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
653 return isl_change_error;
655 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
656 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
657 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
658 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
659 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
660 if (r < 0)
661 return isl_change_error;
663 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
664 if (info[j].ineq[k] != STATUS_VALID)
665 continue;
666 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
667 return isl_change_error;
670 super = contains(&info[j], info[i].tab);
671 if (super < 0)
672 return isl_change_error;
673 if (super)
674 return fuse(i, j, info, NULL, 0, 0);
676 if (isl_tab_rollback(info[i].tab, snap) < 0)
677 return isl_change_error;
679 return isl_change_none;
683 /* Both basic maps have at least one inequality with and adjacent
684 * (but opposite) inequality in the other basic map.
685 * Check that there are no cut constraints and that there is only
686 * a single pair of adjacent inequalities.
687 * If so, we can replace the pair by a single basic map described
688 * by all but the pair of adjacent inequalities.
689 * Any additional points introduced lie strictly between the two
690 * adjacent hyperplanes and can therefore be integral.
692 * ____ _____
693 * / ||\ / \
694 * / || \ / \
695 * \ || \ => \ \
696 * \ || / \ /
697 * \___||_/ \_____/
699 * The test for a single pair of adjancent inequalities is important
700 * for avoiding the combination of two basic maps like the following
702 * /|
703 * / |
704 * /__|
705 * _____
706 * | |
707 * | |
708 * |___|
710 * If there are some cut constraints on one side, then we may
711 * still be able to fuse the two basic maps, but we need to perform
712 * some additional checks in is_adj_ineq_extension.
714 static enum isl_change check_adj_ineq(int i, int j,
715 struct isl_coalesce_info *info)
717 int count_i, count_j;
718 int cut_i, cut_j;
720 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
721 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
723 if (count_i != 1 && count_j != 1)
724 return isl_change_none;
726 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
727 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
728 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
729 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
731 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
732 return fuse(i, j, info, NULL, 0, 0);
734 if (count_i == 1 && !cut_i)
735 return is_adj_ineq_extension(i, j, info);
737 if (count_j == 1 && !cut_j)
738 return is_adj_ineq_extension(j, i, info);
740 return isl_change_none;
743 /* Given an affine transformation matrix "T", does row "row" represent
744 * anything other than a unit vector (possibly shifted by a constant)
745 * that is not involved in any of the other rows?
747 * That is, if a constraint involves the variable corresponding to
748 * the row, then could its preimage by "T" have any coefficients
749 * that are different from those in the original constraint?
751 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
753 int i, j;
754 int len = T->n_col - 1;
756 i = isl_seq_first_non_zero(T->row[row] + 1, len);
757 if (i < 0)
758 return 1;
759 if (!isl_int_is_one(T->row[row][1 + i]) &&
760 !isl_int_is_negone(T->row[row][1 + i]))
761 return 1;
763 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
764 if (j >= 0)
765 return 1;
767 for (j = 1; j < T->n_row; ++j) {
768 if (j == row)
769 continue;
770 if (!isl_int_is_zero(T->row[j][1 + i]))
771 return 1;
774 return 0;
777 /* Does inequality constraint "ineq" of "bmap" involve any of
778 * the variables marked in "affected"?
779 * "total" is the total number of variables, i.e., the number
780 * of entries in "affected".
782 static int is_affected(__isl_keep isl_basic_map *bmap, int ineq, int *affected,
783 int total)
785 int i;
787 for (i = 0; i < total; ++i) {
788 if (!affected[i])
789 continue;
790 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
791 return 1;
794 return 0;
797 /* Given the compressed version of inequality constraint "ineq"
798 * of info->bmap in "v", check if the constraint can be tightened,
799 * where the compression is based on an equality constraint valid
800 * for info->tab.
801 * If so, add the tightened version of the inequality constraint
802 * to info->tab. "v" may be modified by this function.
804 * That is, if the compressed constraint is of the form
806 * m f() + c >= 0
808 * with 0 < c < m, then it is equivalent to
810 * f() >= 0
812 * This means that c can also be subtracted from the original,
813 * uncompressed constraint without affecting the integer points
814 * in info->tab. Add this tightened constraint as an extra row
815 * to info->tab to make this information explicitly available.
817 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
818 int ineq, __isl_take isl_vec *v)
820 isl_ctx *ctx;
821 int r;
823 if (!v)
824 return NULL;
826 ctx = isl_vec_get_ctx(v);
827 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
828 if (isl_int_is_zero(ctx->normalize_gcd) ||
829 isl_int_is_one(ctx->normalize_gcd)) {
830 return v;
833 v = isl_vec_cow(v);
834 if (!v)
835 return NULL;
837 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
838 if (isl_int_is_zero(v->el[0]))
839 return v;
841 if (isl_tab_extend_cons(info->tab, 1) < 0)
842 return isl_vec_free(v);
844 isl_int_sub(info->bmap->ineq[ineq][0],
845 info->bmap->ineq[ineq][0], v->el[0]);
846 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
847 isl_int_add(info->bmap->ineq[ineq][0],
848 info->bmap->ineq[ineq][0], v->el[0]);
850 if (r < 0)
851 return isl_vec_free(v);
853 return v;
856 /* Tighten the (non-redundant) constraints on the facet represented
857 * by info->tab.
858 * In particular, on input, info->tab represents the result
859 * of replacing constraint k of info->bmap, i.e., f_k >= 0,
860 * by the adjacent equality, i.e., f_k + 1 = 0.
862 * Compute a variable compression from the equality constraint f_k + 1 = 0
863 * and use it to tighten the other constraints of info->bmap,
864 * updating info->tab (and leaving info->bmap untouched).
865 * The compression handles essentially two cases, one where a variable
866 * is assigned a fixed value and can therefore be eliminated, and one
867 * where one variable is a shifted multiple of some other variable and
868 * can therefore be replaced by that multiple.
869 * Gaussian elimination would also work for the first case, but for
870 * the second case, the effectiveness would depend on the order
871 * of the variables.
872 * After compression, some of the constraints may have coefficients
873 * with a common divisor. If this divisor does not divide the constant
874 * term, then the constraint can be tightened.
875 * The tightening is performed on the tableau info->tab by introducing
876 * extra (temporary) constraints.
878 * Only constraints that are possibly affected by the compression are
879 * considered. In particular, if the constraint only involves variables
880 * that are directly mapped to a distinct set of other variables, then
881 * no common divisor can be introduced and no tightening can occur.
883 * It is important to only consider the non-redundant constraints
884 * since the facet constraint has been relaxed prior to the call
885 * to this function, meaning that the constraints that were redundant
886 * prior to the relaxation may no longer be redundant.
887 * These constraints will be ignored in the fused result, so
888 * the fusion detection should not exploit them.
890 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
891 int k)
893 unsigned total;
894 isl_ctx *ctx;
895 isl_vec *v = NULL;
896 isl_mat *T;
897 int i;
898 int *affected;
900 ctx = isl_basic_map_get_ctx(info->bmap);
901 total = isl_basic_map_total_dim(info->bmap);
902 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
903 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
904 T = isl_mat_variable_compression(T, NULL);
905 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
906 if (!T)
907 return isl_stat_error;
908 if (T->n_col == 0) {
909 isl_mat_free(T);
910 return isl_stat_ok;
913 affected = isl_alloc_array(ctx, int, total);
914 if (!affected)
915 goto error;
917 for (i = 0; i < total; ++i)
918 affected[i] = not_unique_unit_row(T, 1 + i);
920 for (i = 0; i < info->bmap->n_ineq; ++i) {
921 if (i == k)
922 continue;
923 if (info->ineq[i] == STATUS_REDUNDANT)
924 continue;
925 if (!is_affected(info->bmap, i, affected, total))
926 continue;
927 v = isl_vec_alloc(ctx, 1 + total);
928 if (!v)
929 goto error;
930 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
931 v = isl_vec_mat_product(v, isl_mat_copy(T));
932 v = try_tightening(info, i, v);
933 isl_vec_free(v);
934 if (!v)
935 goto error;
938 isl_mat_free(T);
939 free(affected);
940 return isl_stat_ok;
941 error:
942 isl_mat_free(T);
943 free(affected);
944 return isl_stat_error;
947 /* Basic map "i" has an inequality "k" that is adjacent to some equality
948 * of basic map "j". All the other inequalities are valid for "j".
949 * Check if basic map "j" forms an extension of basic map "i".
951 * In particular, we relax constraint "k", compute the corresponding
952 * facet and check whether it is included in the other basic map.
953 * Before testing for inclusion, the constraints on the facet
954 * are tightened to increase the chance of an inclusion being detected.
955 * If the facet is included, we know that relaxing the constraint extends
956 * the basic map with exactly the other basic map (we already know that this
957 * other basic map is included in the extension, because there
958 * were no "cut" inequalities in "i") and we can replace the
959 * two basic maps by this extension.
960 * Each integer division that does not have exactly the same
961 * definition in "i" and "j" is marked unknown and the basic map
962 * is scheduled to be simplified in an attempt to recover
963 * the integer division definition.
964 * Place this extension in the position that is the smallest of i and j.
965 * ____ _____
966 * / || / |
967 * / || / |
968 * \ || => \ |
969 * \ || \ |
970 * \___|| \____|
972 static enum isl_change is_adj_eq_extension(int i, int j, int k,
973 struct isl_coalesce_info *info)
975 int change = isl_change_none;
976 int super;
977 struct isl_tab_undo *snap, *snap2;
978 unsigned n_eq = info[i].bmap->n_eq;
980 if (isl_tab_is_equality(info[i].tab, n_eq + k))
981 return isl_change_none;
983 snap = isl_tab_snap(info[i].tab);
984 if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
985 return isl_change_error;
986 snap2 = isl_tab_snap(info[i].tab);
987 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
988 return isl_change_error;
989 if (tighten_on_relaxed_facet(&info[i], k) < 0)
990 return isl_change_error;
991 super = contains(&info[j], info[i].tab);
992 if (super < 0)
993 return isl_change_error;
994 if (super) {
995 int l;
996 unsigned total;
998 if (isl_tab_rollback(info[i].tab, snap2) < 0)
999 return isl_change_error;
1000 info[i].bmap = isl_basic_map_cow(info[i].bmap);
1001 if (!info[i].bmap)
1002 return isl_change_error;
1003 total = isl_basic_map_total_dim(info[i].bmap);
1004 for (l = 0; l < info[i].bmap->n_div; ++l)
1005 if (!isl_seq_eq(info[i].bmap->div[l],
1006 info[j].bmap->div[l], 1 + 1 + total)) {
1007 isl_int_set_si(info[i].bmap->div[l][0], 0);
1008 info[i].simplify = 1;
1010 isl_int_add_ui(info[i].bmap->ineq[k][0],
1011 info[i].bmap->ineq[k][0], 1);
1012 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1013 drop(&info[j]);
1014 if (j < i)
1015 exchange(&info[i], &info[j]);
1016 change = isl_change_fuse;
1017 } else
1018 if (isl_tab_rollback(info[i].tab, snap) < 0)
1019 return isl_change_error;
1021 return change;
1024 /* Data structure that keeps track of the wrapping constraints
1025 * and of information to bound the coefficients of those constraints.
1027 * bound is set if we want to apply a bound on the coefficients
1028 * mat contains the wrapping constraints
1029 * max is the bound on the coefficients (if bound is set)
1031 struct isl_wraps {
1032 int bound;
1033 isl_mat *mat;
1034 isl_int max;
1037 /* Update wraps->max to be greater than or equal to the coefficients
1038 * in the equalities and inequalities of info->bmap that can be removed
1039 * if we end up applying wrapping.
1041 static void wraps_update_max(struct isl_wraps *wraps,
1042 struct isl_coalesce_info *info)
1044 int k;
1045 isl_int max_k;
1046 unsigned total = isl_basic_map_total_dim(info->bmap);
1048 isl_int_init(max_k);
1050 for (k = 0; k < info->bmap->n_eq; ++k) {
1051 if (info->eq[2 * k] == STATUS_VALID &&
1052 info->eq[2 * k + 1] == STATUS_VALID)
1053 continue;
1054 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1055 if (isl_int_abs_gt(max_k, wraps->max))
1056 isl_int_set(wraps->max, max_k);
1059 for (k = 0; k < info->bmap->n_ineq; ++k) {
1060 if (info->ineq[k] == STATUS_VALID ||
1061 info->ineq[k] == STATUS_REDUNDANT)
1062 continue;
1063 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1064 if (isl_int_abs_gt(max_k, wraps->max))
1065 isl_int_set(wraps->max, max_k);
1068 isl_int_clear(max_k);
1071 /* Initialize the isl_wraps data structure.
1072 * If we want to bound the coefficients of the wrapping constraints,
1073 * we set wraps->max to the largest coefficient
1074 * in the equalities and inequalities that can be removed if we end up
1075 * applying wrapping.
1077 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1078 struct isl_coalesce_info *info, int i, int j)
1080 isl_ctx *ctx;
1082 wraps->bound = 0;
1083 wraps->mat = mat;
1084 if (!mat)
1085 return;
1086 ctx = isl_mat_get_ctx(mat);
1087 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1088 if (!wraps->bound)
1089 return;
1090 isl_int_init(wraps->max);
1091 isl_int_set_si(wraps->max, 0);
1092 wraps_update_max(wraps, &info[i]);
1093 wraps_update_max(wraps, &info[j]);
1096 /* Free the contents of the isl_wraps data structure.
1098 static void wraps_free(struct isl_wraps *wraps)
1100 isl_mat_free(wraps->mat);
1101 if (wraps->bound)
1102 isl_int_clear(wraps->max);
1105 /* Is the wrapping constraint in row "row" allowed?
1107 * If wraps->bound is set, we check that none of the coefficients
1108 * is greater than wraps->max.
1110 static int allow_wrap(struct isl_wraps *wraps, int row)
1112 int i;
1114 if (!wraps->bound)
1115 return 1;
1117 for (i = 1; i < wraps->mat->n_col; ++i)
1118 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1119 return 0;
1121 return 1;
1124 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1125 * to include "set" and add the result in position "w" of "wraps".
1126 * "len" is the total number of coefficients in "bound" and "ineq".
1127 * Return 1 on success, 0 on failure and -1 on error.
1128 * Wrapping can fail if the result of wrapping is equal to "bound"
1129 * or if we want to bound the sizes of the coefficients and
1130 * the wrapped constraint does not satisfy this bound.
1132 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1133 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1135 isl_seq_cpy(wraps->mat->row[w], bound, len);
1136 if (negate) {
1137 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1138 ineq = wraps->mat->row[w + 1];
1140 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1141 return -1;
1142 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1143 return 0;
1144 if (!allow_wrap(wraps, w))
1145 return 0;
1146 return 1;
1149 /* For each constraint in info->bmap that is not redundant (as determined
1150 * by info->tab) and that is not a valid constraint for the other basic map,
1151 * wrap the constraint around "bound" such that it includes the whole
1152 * set "set" and append the resulting constraint to "wraps".
1153 * Note that the constraints that are valid for the other basic map
1154 * will be added to the combined basic map by default, so there is
1155 * no need to wrap them.
1156 * The caller wrap_in_facets even relies on this function not wrapping
1157 * any constraints that are already valid.
1158 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1159 * wraps->n_row is the number of actual wrapped constraints that have
1160 * been added.
1161 * If any of the wrapping problems results in a constraint that is
1162 * identical to "bound", then this means that "set" is unbounded in such
1163 * way that no wrapping is possible. If this happens then wraps->n_row
1164 * is reset to zero.
1165 * Similarly, if we want to bound the coefficients of the wrapping
1166 * constraints and a newly added wrapping constraint does not
1167 * satisfy the bound, then wraps->n_row is also reset to zero.
1169 static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
1170 isl_int *bound, __isl_keep isl_set *set)
1172 int l, m;
1173 int w;
1174 int added;
1175 isl_basic_map *bmap = info->bmap;
1176 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1178 w = wraps->mat->n_row;
1180 for (l = 0; l < bmap->n_ineq; ++l) {
1181 if (info->ineq[l] == STATUS_VALID ||
1182 info->ineq[l] == STATUS_REDUNDANT)
1183 continue;
1184 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1185 continue;
1186 if (isl_seq_eq(bound, bmap->ineq[l], len))
1187 continue;
1188 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1189 continue;
1191 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1192 if (added < 0)
1193 return -1;
1194 if (!added)
1195 goto unbounded;
1196 ++w;
1198 for (l = 0; l < bmap->n_eq; ++l) {
1199 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1200 continue;
1201 if (isl_seq_eq(bound, bmap->eq[l], len))
1202 continue;
1204 for (m = 0; m < 2; ++m) {
1205 if (info->eq[2 * l + m] == STATUS_VALID)
1206 continue;
1207 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1208 set, !m);
1209 if (added < 0)
1210 return -1;
1211 if (!added)
1212 goto unbounded;
1213 ++w;
1217 wraps->mat->n_row = w;
1218 return 0;
1219 unbounded:
1220 wraps->mat->n_row = 0;
1221 return 0;
1224 /* Check if the constraints in "wraps" from "first" until the last
1225 * are all valid for the basic set represented by "tab".
1226 * If not, wraps->n_row is set to zero.
1228 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1229 struct isl_tab *tab)
1231 int i;
1233 for (i = first; i < wraps->n_row; ++i) {
1234 enum isl_ineq_type type;
1235 type = isl_tab_ineq_type(tab, wraps->row[i]);
1236 if (type == isl_ineq_error)
1237 return -1;
1238 if (type == isl_ineq_redundant)
1239 continue;
1240 wraps->n_row = 0;
1241 return 0;
1244 return 0;
1247 /* Return a set that corresponds to the non-redundant constraints
1248 * (as recorded in tab) of bmap.
1250 * It's important to remove the redundant constraints as some
1251 * of the other constraints may have been modified after the
1252 * constraints were marked redundant.
1253 * In particular, a constraint may have been relaxed.
1254 * Redundant constraints are ignored when a constraint is relaxed
1255 * and should therefore continue to be ignored ever after.
1256 * Otherwise, the relaxation might be thwarted by some of
1257 * these constraints.
1259 * Update the underlying set to ensure that the dimension doesn't change.
1260 * Otherwise the integer divisions could get dropped if the tab
1261 * turns out to be empty.
1263 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1264 struct isl_tab *tab)
1266 isl_basic_set *bset;
1268 bmap = isl_basic_map_copy(bmap);
1269 bset = isl_basic_map_underlying_set(bmap);
1270 bset = isl_basic_set_cow(bset);
1271 bset = isl_basic_set_update_from_tab(bset, tab);
1272 return isl_set_from_basic_set(bset);
1275 /* Wrap the constraints of info->bmap that bound the facet defined
1276 * by inequality "k" around (the opposite of) this inequality to
1277 * include "set". "bound" may be used to store the negated inequality.
1278 * Since the wrapped constraints are not guaranteed to contain the whole
1279 * of info->bmap, we check them in check_wraps.
1280 * If any of the wrapped constraints turn out to be invalid, then
1281 * check_wraps will reset wrap->n_row to zero.
1283 static int add_wraps_around_facet(struct isl_wraps *wraps,
1284 struct isl_coalesce_info *info, int k, isl_int *bound,
1285 __isl_keep isl_set *set)
1287 struct isl_tab_undo *snap;
1288 int n;
1289 unsigned total = isl_basic_map_total_dim(info->bmap);
1291 snap = isl_tab_snap(info->tab);
1293 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1294 return -1;
1295 if (isl_tab_detect_redundant(info->tab) < 0)
1296 return -1;
1298 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1300 n = wraps->mat->n_row;
1301 if (add_wraps(wraps, info, bound, set) < 0)
1302 return -1;
1304 if (isl_tab_rollback(info->tab, snap) < 0)
1305 return -1;
1306 if (check_wraps(wraps->mat, n, info->tab) < 0)
1307 return -1;
1309 return 0;
1312 /* Given a basic set i with a constraint k that is adjacent to
1313 * basic set j, check if we can wrap
1314 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1315 * (always) around their ridges to include the other set.
1316 * If so, replace the pair of basic sets by their union.
1318 * All constraints of i (except k) are assumed to be valid or
1319 * cut constraints for j.
1320 * Wrapping the cut constraints to include basic map j may result
1321 * in constraints that are no longer valid of basic map i
1322 * we have to check that the resulting wrapping constraints are valid for i.
1323 * If "wrap_facet" is not set, then all constraints of i (except k)
1324 * are assumed to be valid for j.
1325 * ____ _____
1326 * / | / \
1327 * / || / |
1328 * \ || => \ |
1329 * \ || \ |
1330 * \___|| \____|
1333 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1334 struct isl_coalesce_info *info, int wrap_facet)
1336 enum isl_change change = isl_change_none;
1337 struct isl_wraps wraps;
1338 isl_ctx *ctx;
1339 isl_mat *mat;
1340 struct isl_set *set_i = NULL;
1341 struct isl_set *set_j = NULL;
1342 struct isl_vec *bound = NULL;
1343 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1345 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1346 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1347 ctx = isl_basic_map_get_ctx(info[i].bmap);
1348 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1349 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1350 1 + total);
1351 wraps_init(&wraps, mat, info, i, j);
1352 bound = isl_vec_alloc(ctx, 1 + total);
1353 if (!set_i || !set_j || !wraps.mat || !bound)
1354 goto error;
1356 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1357 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1359 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1360 wraps.mat->n_row = 1;
1362 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1363 goto error;
1364 if (!wraps.mat->n_row)
1365 goto unbounded;
1367 if (wrap_facet) {
1368 if (add_wraps_around_facet(&wraps, &info[i], k,
1369 bound->el, set_j) < 0)
1370 goto error;
1371 if (!wraps.mat->n_row)
1372 goto unbounded;
1375 change = fuse(i, j, info, wraps.mat, 0, 0);
1377 unbounded:
1378 wraps_free(&wraps);
1380 isl_set_free(set_i);
1381 isl_set_free(set_j);
1383 isl_vec_free(bound);
1385 return change;
1386 error:
1387 wraps_free(&wraps);
1388 isl_vec_free(bound);
1389 isl_set_free(set_i);
1390 isl_set_free(set_j);
1391 return isl_change_error;
1394 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1395 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1396 * add wrapping constraints to wrap.mat for all constraints
1397 * of basic map j that bound the part of basic map j that sticks out
1398 * of the cut constraint.
1399 * "set_i" is the underlying set of basic map i.
1400 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1402 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1403 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1404 * (with respect to the integer points), so we add t(x) >= 0 instead.
1405 * Otherwise, we wrap the constraints of basic map j that are not
1406 * redundant in this intersection and that are not already valid
1407 * for basic map i over basic map i.
1408 * Note that it is sufficient to wrap the constraints to include
1409 * basic map i, because we will only wrap the constraints that do
1410 * not include basic map i already. The wrapped constraint will
1411 * therefore be more relaxed compared to the original constraint.
1412 * Since the original constraint is valid for basic map j, so is
1413 * the wrapped constraint.
1415 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1416 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1417 struct isl_tab_undo *snap)
1419 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1420 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1421 return isl_stat_error;
1422 if (isl_tab_detect_redundant(info_j->tab) < 0)
1423 return isl_stat_error;
1425 if (info_j->tab->empty)
1426 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1427 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1428 return isl_stat_error;
1430 if (isl_tab_rollback(info_j->tab, snap) < 0)
1431 return isl_stat_error;
1433 return isl_stat_ok;
1436 /* Given a pair of basic maps i and j such that j sticks out
1437 * of i at n cut constraints, each time by at most one,
1438 * try to compute wrapping constraints and replace the two
1439 * basic maps by a single basic map.
1440 * The other constraints of i are assumed to be valid for j.
1441 * "set_i" is the underlying set of basic map i.
1442 * "wraps" has been initialized to be of the right size.
1444 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1445 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1446 * of basic map j that bound the part of basic map j that sticks out
1447 * of the cut constraint.
1449 * If any wrapping fails, i.e., if we cannot wrap to touch
1450 * the union, then we give up.
1451 * Otherwise, the pair of basic maps is replaced by their union.
1453 static enum isl_change try_wrap_in_facets(int i, int j,
1454 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1455 __isl_keep isl_set *set_i)
1457 int k, l, w;
1458 unsigned total;
1459 struct isl_tab_undo *snap;
1461 total = isl_basic_map_total_dim(info[i].bmap);
1463 snap = isl_tab_snap(info[j].tab);
1465 wraps->mat->n_row = 0;
1467 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1468 for (l = 0; l < 2; ++l) {
1469 if (info[i].eq[2 * k + l] != STATUS_CUT)
1470 continue;
1471 w = wraps->mat->n_row++;
1472 if (l == 0)
1473 isl_seq_neg(wraps->mat->row[w],
1474 info[i].bmap->eq[k], 1 + total);
1475 else
1476 isl_seq_cpy(wraps->mat->row[w],
1477 info[i].bmap->eq[k], 1 + total);
1478 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1479 return isl_change_error;
1481 if (!wraps->mat->n_row)
1482 return isl_change_none;
1486 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1487 if (info[i].ineq[k] != STATUS_CUT)
1488 continue;
1489 w = wraps->mat->n_row++;
1490 isl_seq_cpy(wraps->mat->row[w],
1491 info[i].bmap->ineq[k], 1 + total);
1492 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1493 return isl_change_error;
1495 if (!wraps->mat->n_row)
1496 return isl_change_none;
1499 return fuse(i, j, info, wraps->mat, 0, 1);
1502 /* Given a pair of basic maps i and j such that j sticks out
1503 * of i at n cut constraints, each time by at most one,
1504 * try to compute wrapping constraints and replace the two
1505 * basic maps by a single basic map.
1506 * The other constraints of i are assumed to be valid for j.
1508 * The core computation is performed by try_wrap_in_facets.
1509 * This function simply extracts an underlying set representation
1510 * of basic map i and initializes the data structure for keeping
1511 * track of wrapping constraints.
1513 static enum isl_change wrap_in_facets(int i, int j, int n,
1514 struct isl_coalesce_info *info)
1516 enum isl_change change = isl_change_none;
1517 struct isl_wraps wraps;
1518 isl_ctx *ctx;
1519 isl_mat *mat;
1520 isl_set *set_i = NULL;
1521 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1522 int max_wrap;
1524 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1525 return isl_change_error;
1527 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1528 max_wrap *= n;
1530 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1531 ctx = isl_basic_map_get_ctx(info[i].bmap);
1532 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1533 wraps_init(&wraps, mat, info, i, j);
1534 if (!set_i || !wraps.mat)
1535 goto error;
1537 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1539 wraps_free(&wraps);
1540 isl_set_free(set_i);
1542 return change;
1543 error:
1544 wraps_free(&wraps);
1545 isl_set_free(set_i);
1546 return isl_change_error;
1549 /* Return the effect of inequality "ineq" on the tableau "tab",
1550 * after relaxing the constant term of "ineq" by one.
1552 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1554 enum isl_ineq_type type;
1556 isl_int_add_ui(ineq[0], ineq[0], 1);
1557 type = isl_tab_ineq_type(tab, ineq);
1558 isl_int_sub_ui(ineq[0], ineq[0], 1);
1560 return type;
1563 /* Given two basic sets i and j,
1564 * check if relaxing all the cut constraints of i by one turns
1565 * them into valid constraint for j and check if we can wrap in
1566 * the bits that are sticking out.
1567 * If so, replace the pair by their union.
1569 * We first check if all relaxed cut inequalities of i are valid for j
1570 * and then try to wrap in the intersections of the relaxed cut inequalities
1571 * with j.
1573 * During this wrapping, we consider the points of j that lie at a distance
1574 * of exactly 1 from i. In particular, we ignore the points that lie in
1575 * between this lower-dimensional space and the basic map i.
1576 * We can therefore only apply this to integer maps.
1577 * ____ _____
1578 * / ___|_ / \
1579 * / | | / |
1580 * \ | | => \ |
1581 * \|____| \ |
1582 * \___| \____/
1584 * _____ ______
1585 * | ____|_ | \
1586 * | | | | |
1587 * | | | => | |
1588 * |_| | | |
1589 * |_____| \______|
1591 * _______
1592 * | |
1593 * | |\ |
1594 * | | \ |
1595 * | | \ |
1596 * | | \|
1597 * | | \
1598 * | |_____\
1599 * | |
1600 * |_______|
1602 * Wrapping can fail if the result of wrapping one of the facets
1603 * around its edges does not produce any new facet constraint.
1604 * In particular, this happens when we try to wrap in unbounded sets.
1606 * _______________________________________________________________________
1608 * | ___
1609 * | | |
1610 * |_| |_________________________________________________________________
1611 * |___|
1613 * The following is not an acceptable result of coalescing the above two
1614 * sets as it includes extra integer points.
1615 * _______________________________________________________________________
1617 * |
1618 * |
1620 * \______________________________________________________________________
1622 static enum isl_change can_wrap_in_set(int i, int j,
1623 struct isl_coalesce_info *info)
1625 int k, l;
1626 int n;
1627 unsigned total;
1629 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1630 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1631 return isl_change_none;
1633 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1634 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1635 if (n == 0)
1636 return isl_change_none;
1638 total = isl_basic_map_total_dim(info[i].bmap);
1639 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1640 for (l = 0; l < 2; ++l) {
1641 enum isl_ineq_type type;
1643 if (info[i].eq[2 * k + l] != STATUS_CUT)
1644 continue;
1646 if (l == 0)
1647 isl_seq_neg(info[i].bmap->eq[k],
1648 info[i].bmap->eq[k], 1 + total);
1649 type = type_of_relaxed(info[j].tab,
1650 info[i].bmap->eq[k]);
1651 if (l == 0)
1652 isl_seq_neg(info[i].bmap->eq[k],
1653 info[i].bmap->eq[k], 1 + total);
1654 if (type == isl_ineq_error)
1655 return isl_change_error;
1656 if (type != isl_ineq_redundant)
1657 return isl_change_none;
1661 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1662 enum isl_ineq_type type;
1664 if (info[i].ineq[k] != STATUS_CUT)
1665 continue;
1667 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1668 if (type == isl_ineq_error)
1669 return isl_change_error;
1670 if (type != isl_ineq_redundant)
1671 return isl_change_none;
1674 return wrap_in_facets(i, j, n, info);
1677 /* Check if either i or j has only cut constraints that can
1678 * be used to wrap in (a facet of) the other basic set.
1679 * if so, replace the pair by their union.
1681 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1683 enum isl_change change = isl_change_none;
1685 change = can_wrap_in_set(i, j, info);
1686 if (change != isl_change_none)
1687 return change;
1689 change = can_wrap_in_set(j, i, info);
1690 return change;
1693 /* At least one of the basic maps has an equality that is adjacent
1694 * to inequality. Make sure that only one of the basic maps has
1695 * such an equality and that the other basic map has exactly one
1696 * inequality adjacent to an equality.
1697 * If the other basic map does not have such an inequality, then
1698 * check if all its constraints are either valid or cut constraints
1699 * and, if so, try wrapping in the first map into the second.
1701 * We call the basic map that has the inequality "i" and the basic
1702 * map that has the equality "j".
1703 * If "i" has any "cut" (in)equality, then relaxing the inequality
1704 * by one would not result in a basic map that contains the other
1705 * basic map. However, it may still be possible to wrap in the other
1706 * basic map.
1708 static enum isl_change check_adj_eq(int i, int j,
1709 struct isl_coalesce_info *info)
1711 enum isl_change change = isl_change_none;
1712 int k;
1713 int any_cut;
1715 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1716 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1717 /* ADJ EQ TOO MANY */
1718 return isl_change_none;
1720 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1721 return check_adj_eq(j, i, info);
1723 /* j has an equality adjacent to an inequality in i */
1725 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1726 if (all_valid_or_cut(&info[i]))
1727 return can_wrap_in_set(i, j, info);
1728 return isl_change_none;
1730 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1731 return isl_change_none;
1732 any_cut = any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1733 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1734 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1735 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1736 /* ADJ EQ TOO MANY */
1737 return isl_change_none;
1739 for (k = 0; k < info[i].bmap->n_ineq; ++k)
1740 if (info[i].ineq[k] == STATUS_ADJ_EQ)
1741 break;
1743 if (!any_cut) {
1744 change = is_adj_eq_extension(i, j, k, info);
1745 if (change != isl_change_none)
1746 return change;
1749 change = can_wrap_in_facet(i, j, k, info, any_cut);
1751 return change;
1754 /* The two basic maps lie on adjacent hyperplanes. In particular,
1755 * basic map "i" has an equality that lies parallel to basic map "j".
1756 * Check if we can wrap the facets around the parallel hyperplanes
1757 * to include the other set.
1759 * We perform basically the same operations as can_wrap_in_facet,
1760 * except that we don't need to select a facet of one of the sets.
1762 * \\ \\
1763 * \\ => \\
1764 * \ \|
1766 * If there is more than one equality of "i" adjacent to an equality of "j",
1767 * then the result will satisfy one or more equalities that are a linear
1768 * combination of these equalities. These will be encoded as pairs
1769 * of inequalities in the wrapping constraints and need to be made
1770 * explicit.
1772 static enum isl_change check_eq_adj_eq(int i, int j,
1773 struct isl_coalesce_info *info)
1775 int k;
1776 enum isl_change change = isl_change_none;
1777 int detect_equalities = 0;
1778 struct isl_wraps wraps;
1779 isl_ctx *ctx;
1780 isl_mat *mat;
1781 struct isl_set *set_i = NULL;
1782 struct isl_set *set_j = NULL;
1783 struct isl_vec *bound = NULL;
1784 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1786 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1787 detect_equalities = 1;
1789 for (k = 0; k < 2 * info[i].bmap->n_eq ; ++k)
1790 if (info[i].eq[k] == STATUS_ADJ_EQ)
1791 break;
1793 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1794 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1795 ctx = isl_basic_map_get_ctx(info[i].bmap);
1796 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1797 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1798 1 + total);
1799 wraps_init(&wraps, mat, info, i, j);
1800 bound = isl_vec_alloc(ctx, 1 + total);
1801 if (!set_i || !set_j || !wraps.mat || !bound)
1802 goto error;
1804 if (k % 2 == 0)
1805 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1806 else
1807 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1808 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1810 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1811 wraps.mat->n_row = 1;
1813 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1814 goto error;
1815 if (!wraps.mat->n_row)
1816 goto unbounded;
1818 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1819 isl_seq_neg(bound->el, bound->el, 1 + total);
1821 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1822 wraps.mat->n_row++;
1824 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1825 goto error;
1826 if (!wraps.mat->n_row)
1827 goto unbounded;
1829 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1831 if (0) {
1832 error: change = isl_change_error;
1834 unbounded:
1836 wraps_free(&wraps);
1837 isl_set_free(set_i);
1838 isl_set_free(set_j);
1839 isl_vec_free(bound);
1841 return change;
1844 /* Initialize the "eq" and "ineq" fields of "info".
1846 static void init_status(struct isl_coalesce_info *info)
1848 info->eq = info->ineq = NULL;
1851 /* Set info->eq to the positions of the equalities of info->bmap
1852 * with respect to the basic map represented by "tab".
1853 * If info->eq has already been computed, then do not compute it again.
1855 static void set_eq_status_in(struct isl_coalesce_info *info,
1856 struct isl_tab *tab)
1858 if (info->eq)
1859 return;
1860 info->eq = eq_status_in(info->bmap, tab);
1863 /* Set info->ineq to the positions of the inequalities of info->bmap
1864 * with respect to the basic map represented by "tab".
1865 * If info->ineq has already been computed, then do not compute it again.
1867 static void set_ineq_status_in(struct isl_coalesce_info *info,
1868 struct isl_tab *tab)
1870 if (info->ineq)
1871 return;
1872 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
1875 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
1876 * This function assumes that init_status has been called on "info" first,
1877 * after which the "eq" and "ineq" fields may or may not have been
1878 * assigned a newly allocated array.
1880 static void clear_status(struct isl_coalesce_info *info)
1882 free(info->eq);
1883 free(info->ineq);
1886 /* Check if the union of the given pair of basic maps
1887 * can be represented by a single basic map.
1888 * If so, replace the pair by the single basic map and return
1889 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
1890 * Otherwise, return isl_change_none.
1891 * The two basic maps are assumed to live in the same local space.
1892 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
1893 * to have been initialized by the caller, either to NULL or
1894 * to valid information.
1896 * We first check the effect of each constraint of one basic map
1897 * on the other basic map.
1898 * The constraint may be
1899 * redundant the constraint is redundant in its own
1900 * basic map and should be ignore and removed
1901 * in the end
1902 * valid all (integer) points of the other basic map
1903 * satisfy the constraint
1904 * separate no (integer) point of the other basic map
1905 * satisfies the constraint
1906 * cut some but not all points of the other basic map
1907 * satisfy the constraint
1908 * adj_eq the given constraint is adjacent (on the outside)
1909 * to an equality of the other basic map
1910 * adj_ineq the given constraint is adjacent (on the outside)
1911 * to an inequality of the other basic map
1913 * We consider seven cases in which we can replace the pair by a single
1914 * basic map. We ignore all "redundant" constraints.
1916 * 1. all constraints of one basic map are valid
1917 * => the other basic map is a subset and can be removed
1919 * 2. all constraints of both basic maps are either "valid" or "cut"
1920 * and the facets corresponding to the "cut" constraints
1921 * of one of the basic maps lies entirely inside the other basic map
1922 * => the pair can be replaced by a basic map consisting
1923 * of the valid constraints in both basic maps
1925 * 3. there is a single pair of adjacent inequalities
1926 * (all other constraints are "valid")
1927 * => the pair can be replaced by a basic map consisting
1928 * of the valid constraints in both basic maps
1930 * 4. one basic map has a single adjacent inequality, while the other
1931 * constraints are "valid". The other basic map has some
1932 * "cut" constraints, but replacing the adjacent inequality by
1933 * its opposite and adding the valid constraints of the other
1934 * basic map results in a subset of the other basic map
1935 * => the pair can be replaced by a basic map consisting
1936 * of the valid constraints in both basic maps
1938 * 5. there is a single adjacent pair of an inequality and an equality,
1939 * the other constraints of the basic map containing the inequality are
1940 * "valid". Moreover, if the inequality the basic map is relaxed
1941 * and then turned into an equality, then resulting facet lies
1942 * entirely inside the other basic map
1943 * => the pair can be replaced by the basic map containing
1944 * the inequality, with the inequality relaxed.
1946 * 6. there is a single adjacent pair of an inequality and an equality,
1947 * the other constraints of the basic map containing the inequality are
1948 * "valid". Moreover, the facets corresponding to both
1949 * the inequality and the equality can be wrapped around their
1950 * ridges to include the other basic map
1951 * => the pair can be replaced by a basic map consisting
1952 * of the valid constraints in both basic maps together
1953 * with all wrapping constraints
1955 * 7. one of the basic maps extends beyond the other by at most one.
1956 * Moreover, the facets corresponding to the cut constraints and
1957 * the pieces of the other basic map at offset one from these cut
1958 * constraints can be wrapped around their ridges to include
1959 * the union of the two basic maps
1960 * => the pair can be replaced by a basic map consisting
1961 * of the valid constraints in both basic maps together
1962 * with all wrapping constraints
1964 * 8. the two basic maps live in adjacent hyperplanes. In principle
1965 * such sets can always be combined through wrapping, but we impose
1966 * that there is only one such pair, to avoid overeager coalescing.
1968 * Throughout the computation, we maintain a collection of tableaus
1969 * corresponding to the basic maps. When the basic maps are dropped
1970 * or combined, the tableaus are modified accordingly.
1972 static enum isl_change coalesce_local_pair_reuse(int i, int j,
1973 struct isl_coalesce_info *info)
1975 enum isl_change change = isl_change_none;
1977 set_eq_status_in(&info[i], info[j].tab);
1978 if (info[i].bmap->n_eq && !info[i].eq)
1979 goto error;
1980 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
1981 goto error;
1982 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
1983 goto done;
1985 set_eq_status_in(&info[j], info[i].tab);
1986 if (info[j].bmap->n_eq && !info[j].eq)
1987 goto error;
1988 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
1989 goto error;
1990 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
1991 goto done;
1993 set_ineq_status_in(&info[i], info[j].tab);
1994 if (info[i].bmap->n_ineq && !info[i].ineq)
1995 goto error;
1996 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
1997 goto error;
1998 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
1999 goto done;
2001 set_ineq_status_in(&info[j], info[i].tab);
2002 if (info[j].bmap->n_ineq && !info[j].ineq)
2003 goto error;
2004 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2005 goto error;
2006 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2007 goto done;
2009 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2010 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2011 drop(&info[j]);
2012 change = isl_change_drop_second;
2013 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2014 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2015 drop(&info[i]);
2016 change = isl_change_drop_first;
2017 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2018 change = check_eq_adj_eq(i, j, info);
2019 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2020 change = check_eq_adj_eq(j, i, info);
2021 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2022 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2023 change = check_adj_eq(i, j, info);
2024 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2025 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2026 /* Can't happen */
2027 /* BAD ADJ INEQ */
2028 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2029 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2030 change = check_adj_ineq(i, j, info);
2031 } else {
2032 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2033 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2034 change = check_facets(i, j, info);
2035 if (change == isl_change_none)
2036 change = check_wrap(i, j, info);
2039 done:
2040 clear_status(&info[i]);
2041 clear_status(&info[j]);
2042 return change;
2043 error:
2044 clear_status(&info[i]);
2045 clear_status(&info[j]);
2046 return isl_change_error;
2049 /* Check if the union of the given pair of basic maps
2050 * can be represented by a single basic map.
2051 * If so, replace the pair by the single basic map and return
2052 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2053 * Otherwise, return isl_change_none.
2054 * The two basic maps are assumed to live in the same local space.
2056 static enum isl_change coalesce_local_pair(int i, int j,
2057 struct isl_coalesce_info *info)
2059 init_status(&info[i]);
2060 init_status(&info[j]);
2061 return coalesce_local_pair_reuse(i, j, info);
2064 /* Shift the integer division at position "div" of the basic map
2065 * represented by "info" by "shift".
2067 * That is, if the integer division has the form
2069 * floor(f(x)/d)
2071 * then replace it by
2073 * floor((f(x) + shift * d)/d) - shift
2075 static int shift_div(struct isl_coalesce_info *info, int div, isl_int shift)
2077 unsigned total;
2079 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2080 if (!info->bmap)
2081 return -1;
2083 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2084 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2085 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2086 return -1;
2088 return 0;
2091 /* Check if some of the divs in the basic map represented by "info1"
2092 * are shifts of the corresponding divs in the basic map represented
2093 * by "info2". If so, align them with those of "info2".
2094 * Only do this if "info1" and "info2" have the same number
2095 * of integer divisions.
2097 * An integer division is considered to be a shift of another integer
2098 * division if one is equal to the other plus a constant.
2100 * In particular, for each pair of integer divisions, if both are known,
2101 * have identical coefficients (apart from the constant term) and
2102 * if the difference between the constant terms (taking into account
2103 * the denominator) is an integer, then move the difference outside.
2104 * That is, if one integer division is of the form
2106 * floor((f(x) + c_1)/d)
2108 * while the other is of the form
2110 * floor((f(x) + c_2)/d)
2112 * and n = (c_2 - c_1)/d is an integer, then replace the first
2113 * integer division by
2115 * floor((f(x) + c_1 + n * d)/d) - n = floor((f(x) + c_2)/d) - n
2117 static int harmonize_divs(struct isl_coalesce_info *info1,
2118 struct isl_coalesce_info *info2)
2120 int i;
2121 int total;
2123 if (!info1->bmap || !info2->bmap)
2124 return -1;
2126 if (info1->bmap->n_div != info2->bmap->n_div)
2127 return 0;
2128 if (info1->bmap->n_div == 0)
2129 return 0;
2131 total = isl_basic_map_total_dim(info1->bmap);
2132 for (i = 0; i < info1->bmap->n_div; ++i) {
2133 isl_int d;
2134 int r = 0;
2136 if (isl_int_is_zero(info1->bmap->div[i][0]) ||
2137 isl_int_is_zero(info2->bmap->div[i][0]))
2138 continue;
2139 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2140 continue;
2141 if (isl_int_eq(info1->bmap->div[i][1], info2->bmap->div[i][1]))
2142 continue;
2143 if (!isl_seq_eq(info1->bmap->div[i] + 2,
2144 info2->bmap->div[i] + 2, total))
2145 continue;
2146 isl_int_init(d);
2147 isl_int_sub(d, info2->bmap->div[i][1], info1->bmap->div[i][1]);
2148 if (isl_int_is_divisible_by(d, info1->bmap->div[i][0])) {
2149 isl_int_divexact(d, d, info1->bmap->div[i][0]);
2150 r = shift_div(info1, i, d);
2152 isl_int_clear(d);
2153 if (r < 0)
2154 return -1;
2157 return 0;
2160 /* Do the two basic maps live in the same local space, i.e.,
2161 * do they have the same (known) divs?
2162 * If either basic map has any unknown divs, then we can only assume
2163 * that they do not live in the same local space.
2165 static int same_divs(__isl_keep isl_basic_map *bmap1,
2166 __isl_keep isl_basic_map *bmap2)
2168 int i;
2169 int known;
2170 int total;
2172 if (!bmap1 || !bmap2)
2173 return -1;
2174 if (bmap1->n_div != bmap2->n_div)
2175 return 0;
2177 if (bmap1->n_div == 0)
2178 return 1;
2180 known = isl_basic_map_divs_known(bmap1);
2181 if (known < 0 || !known)
2182 return known;
2183 known = isl_basic_map_divs_known(bmap2);
2184 if (known < 0 || !known)
2185 return known;
2187 total = isl_basic_map_total_dim(bmap1);
2188 for (i = 0; i < bmap1->n_div; ++i)
2189 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2190 return 0;
2192 return 1;
2195 /* Expand info->tab in the same way info->bmap was expanded in
2196 * isl_basic_map_expand_divs using the expansion "exp" and
2197 * update info->ineq with respect to the redundant constraints
2198 * in the resulting tableau. "bmap" is the original version
2199 * of info->bmap, i.e., the one that corresponds to the current
2200 * state of info->tab. The number of constraints in "bmap"
2201 * is assumed to be the same as the number of constraints
2202 * in info->tab. This is required to be able to detect
2203 * the extra constraints in info->bmap.
2205 * In particular, introduce extra variables corresponding
2206 * to the extra integer divisions and add the div constraints
2207 * that were added to info->bmap after info->tab was created
2208 * from the original info->bmap.
2209 * info->ineq was computed without a tableau and therefore
2210 * does not take into account the redundant constraints
2211 * in the tableau. Mark them here.
2213 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2214 __isl_keep isl_basic_map *bmap)
2216 unsigned total, pos, n_div;
2217 int extra_var;
2218 int i, n, j, n_ineq;
2219 unsigned n_eq;
2221 if (!bmap)
2222 return isl_stat_error;
2223 if (bmap->n_eq + bmap->n_ineq != info->tab->n_con)
2224 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2225 "original tableau does not correspond "
2226 "to original basic map", return isl_stat_error);
2228 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2229 n_div = isl_basic_map_dim(info->bmap, isl_dim_div);
2230 pos = total - n_div;
2231 extra_var = total - info->tab->n_var;
2232 n = n_div - extra_var;
2234 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2235 return isl_stat_error;
2236 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2237 return isl_stat_error;
2239 i = 0;
2240 for (j = 0; j < n_div; ++j) {
2241 if (i < n && exp[i] == j) {
2242 ++i;
2243 continue;
2245 if (isl_tab_insert_var(info->tab, pos + j) < 0)
2246 return isl_stat_error;
2249 n_ineq = info->tab->n_con - info->tab->n_eq;
2250 for (i = n_ineq; i < info->bmap->n_ineq; ++i)
2251 if (isl_tab_add_ineq(info->tab, info->bmap->ineq[i]) < 0)
2252 return isl_stat_error;
2254 n_eq = info->bmap->n_eq;
2255 for (i = 0; i < info->bmap->n_ineq; ++i) {
2256 if (isl_tab_is_redundant(info->tab, n_eq + i))
2257 info->ineq[i] = STATUS_REDUNDANT;
2260 return isl_stat_ok;
2263 /* Check if the union of the basic maps represented by info[i] and info[j]
2264 * can be represented by a single basic map,
2265 * after expanding the divs of info[i] to match those of info[j].
2266 * If so, replace the pair by the single basic map and return
2267 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2268 * Otherwise, return isl_change_none.
2270 * The caller has already checked for info[j] being a subset of info[i].
2271 * If some of the divs of info[j] are unknown, then the expanded info[i]
2272 * will not have the corresponding div constraints. The other patterns
2273 * therefore cannot apply. Skip the computation in this case.
2275 * The expansion is performed using the divs "div" and expansion "exp"
2276 * computed by the caller.
2277 * info[i].bmap has already been expanded and the result is passed in
2278 * as "bmap".
2279 * The "eq" and "ineq" fields of info[i] reflect the status of
2280 * the constraints of the expanded "bmap" with respect to info[j].tab.
2281 * However, inequality constraints that are redundant in info[i].tab
2282 * have not yet been marked as such because no tableau was available.
2284 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2285 * updating info[i].ineq with respect to the redundant constraints.
2286 * Then try and coalesce the expanded info[i] with info[j],
2287 * reusing the information in info[i].eq and info[i].ineq.
2288 * If this does not result in any coalescing or if it results in info[j]
2289 * getting dropped (which should not happen in practice, since the case
2290 * of info[j] being a subset of info[i] has already been checked by
2291 * the caller), then revert info[i] to its original state.
2293 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2294 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2295 int *exp)
2297 isl_bool known;
2298 isl_basic_map *bmap_i;
2299 struct isl_tab_undo *snap;
2300 enum isl_change change = isl_change_none;
2302 known = isl_basic_map_divs_known(info[j].bmap);
2303 if (known < 0 || !known) {
2304 clear_status(&info[i]);
2305 isl_basic_map_free(bmap);
2306 return known < 0 ? isl_change_error : isl_change_none;
2309 bmap_i = info[i].bmap;
2310 info[i].bmap = isl_basic_map_copy(bmap);
2311 snap = isl_tab_snap(info[i].tab);
2312 if (!info[i].bmap || expand_tab(&info[i], exp, bmap_i) < 0)
2313 change = isl_change_error;
2315 init_status(&info[j]);
2316 if (change == isl_change_none)
2317 change = coalesce_local_pair_reuse(i, j, info);
2318 else
2319 clear_status(&info[i]);
2320 if (change != isl_change_none && change != isl_change_drop_second) {
2321 isl_basic_map_free(bmap_i);
2322 } else {
2323 isl_basic_map_free(info[i].bmap);
2324 info[i].bmap = bmap_i;
2326 if (isl_tab_rollback(info[i].tab, snap) < 0)
2327 change = isl_change_error;
2330 isl_basic_map_free(bmap);
2331 return change;
2334 /* Check if the union of "bmap" and the basic map represented by info[j]
2335 * can be represented by a single basic map,
2336 * after expanding the divs of "bmap" to match those of info[j].
2337 * If so, replace the pair by the single basic map and return
2338 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2339 * Otherwise, return isl_change_none.
2341 * In particular, check if the expanded "bmap" contains the basic map
2342 * represented by the tableau info[j].tab.
2343 * The expansion is performed using the divs "div" and expansion "exp"
2344 * computed by the caller.
2345 * Then we check if all constraints of the expanded "bmap" are valid for
2346 * info[j].tab.
2348 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2349 * In this case, the positions of the constraints of info[i].bmap
2350 * with respect to the basic map represented by info[j] are stored
2351 * in info[i].
2353 * If the expanded "bmap" does not contain the basic map
2354 * represented by the tableau info[j].tab and if "i" is not -1,
2355 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2356 * as well and check if that results in coalescing.
2358 static enum isl_change coalesce_with_expanded_divs(
2359 __isl_keep isl_basic_map *bmap, int i, int j,
2360 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2362 enum isl_change change = isl_change_none;
2363 struct isl_coalesce_info info_local, *info_i;
2365 info_i = i >= 0 ? &info[i] : &info_local;
2366 init_status(info_i);
2367 bmap = isl_basic_map_copy(bmap);
2368 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2370 if (!bmap)
2371 goto error;
2373 info_i->eq = eq_status_in(bmap, info[j].tab);
2374 if (bmap->n_eq && !info_i->eq)
2375 goto error;
2376 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2377 goto error;
2378 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2379 goto done;
2381 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2382 if (bmap->n_ineq && !info_i->ineq)
2383 goto error;
2384 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2385 goto error;
2386 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2387 goto done;
2389 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2390 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2391 drop(&info[j]);
2392 change = isl_change_drop_second;
2395 if (change == isl_change_none && i != -1)
2396 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2398 done:
2399 isl_basic_map_free(bmap);
2400 clear_status(info_i);
2401 return change;
2402 error:
2403 isl_basic_map_free(bmap);
2404 clear_status(info_i);
2405 return isl_change_error;
2408 /* Check if the union of "bmap_i" and the basic map represented by info[j]
2409 * can be represented by a single basic map,
2410 * after aligning the divs of "bmap_i" to match those of info[j].
2411 * If so, replace the pair by the single basic map and return
2412 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2413 * Otherwise, return isl_change_none.
2415 * In particular, check if "bmap_i" contains the basic map represented by
2416 * info[j] after aligning the divs of "bmap_i" to those of info[j].
2417 * Note that this can only succeed if the number of divs of "bmap_i"
2418 * is smaller than (or equal to) the number of divs of info[j].
2420 * We first check if the divs of "bmap_i" are all known and form a subset
2421 * of those of info[j].bmap. If so, we pass control over to
2422 * coalesce_with_expanded_divs.
2424 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2426 static enum isl_change coalesce_after_aligning_divs(
2427 __isl_keep isl_basic_map *bmap_i, int i, int j,
2428 struct isl_coalesce_info *info)
2430 int known;
2431 isl_mat *div_i, *div_j, *div;
2432 int *exp1 = NULL;
2433 int *exp2 = NULL;
2434 isl_ctx *ctx;
2435 enum isl_change change;
2437 known = isl_basic_map_divs_known(bmap_i);
2438 if (known < 0 || !known)
2439 return known;
2441 ctx = isl_basic_map_get_ctx(bmap_i);
2443 div_i = isl_basic_map_get_divs(bmap_i);
2444 div_j = isl_basic_map_get_divs(info[j].bmap);
2446 if (!div_i || !div_j)
2447 goto error;
2449 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
2450 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
2451 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
2452 goto error;
2454 div = isl_merge_divs(div_i, div_j, exp1, exp2);
2455 if (!div)
2456 goto error;
2458 if (div->n_row == div_j->n_row)
2459 change = coalesce_with_expanded_divs(bmap_i,
2460 i, j, info, div, exp1);
2461 else
2462 change = isl_change_none;
2464 isl_mat_free(div);
2466 isl_mat_free(div_i);
2467 isl_mat_free(div_j);
2469 free(exp2);
2470 free(exp1);
2472 return change;
2473 error:
2474 isl_mat_free(div_i);
2475 isl_mat_free(div_j);
2476 free(exp1);
2477 free(exp2);
2478 return isl_change_error;
2481 /* Check if basic map "j" is a subset of basic map "i" after
2482 * exploiting the extra equalities of "j" to simplify the divs of "i".
2483 * If so, remove basic map "j" and return isl_change_drop_second.
2485 * If "j" does not have any equalities or if they are the same
2486 * as those of "i", then we cannot exploit them to simplify the divs.
2487 * Similarly, if there are no divs in "i", then they cannot be simplified.
2488 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
2489 * then "j" cannot be a subset of "i".
2491 * Otherwise, we intersect "i" with the affine hull of "j" and then
2492 * check if "j" is a subset of the result after aligning the divs.
2493 * If so, then "j" is definitely a subset of "i" and can be removed.
2494 * Note that if after intersection with the affine hull of "j".
2495 * "i" still has more divs than "j", then there is no way we can
2496 * align the divs of "i" to those of "j".
2498 static enum isl_change coalesce_subset_with_equalities(int i, int j,
2499 struct isl_coalesce_info *info)
2501 isl_basic_map *hull_i, *hull_j, *bmap_i;
2502 int equal, empty;
2503 enum isl_change change;
2505 if (info[j].bmap->n_eq == 0)
2506 return isl_change_none;
2507 if (info[i].bmap->n_div == 0)
2508 return isl_change_none;
2510 hull_i = isl_basic_map_copy(info[i].bmap);
2511 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2512 hull_j = isl_basic_map_copy(info[j].bmap);
2513 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2515 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2516 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2517 empty = isl_basic_map_plain_is_empty(hull_j);
2518 isl_basic_map_free(hull_i);
2520 if (equal < 0 || equal || empty < 0 || empty) {
2521 isl_basic_map_free(hull_j);
2522 if (equal < 0 || empty < 0)
2523 return isl_change_error;
2524 return isl_change_none;
2527 bmap_i = isl_basic_map_copy(info[i].bmap);
2528 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
2529 if (!bmap_i)
2530 return isl_change_error;
2532 if (bmap_i->n_div > info[j].bmap->n_div) {
2533 isl_basic_map_free(bmap_i);
2534 return isl_change_none;
2537 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
2539 isl_basic_map_free(bmap_i);
2541 return change;
2544 /* Check if the union of and the basic maps represented by info[i] and info[j]
2545 * can be represented by a single basic map, by aligning or equating
2546 * their integer divisions.
2547 * If so, replace the pair by the single basic map and return
2548 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2549 * Otherwise, return isl_change_none.
2551 * Note that we only perform any test if the number of divs is different
2552 * in the two basic maps. In case the number of divs is the same,
2553 * we have already established that the divs are different
2554 * in the two basic maps.
2555 * In particular, if the number of divs of basic map i is smaller than
2556 * the number of divs of basic map j, then we check if j is a subset of i
2557 * and vice versa.
2559 static enum isl_change coalesce_divs(int i, int j,
2560 struct isl_coalesce_info *info)
2562 enum isl_change change = isl_change_none;
2564 if (info[i].bmap->n_div < info[j].bmap->n_div)
2565 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
2566 if (change != isl_change_none)
2567 return change;
2569 if (info[j].bmap->n_div < info[i].bmap->n_div)
2570 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
2571 if (change != isl_change_none)
2572 return invert_change(change);
2574 change = coalesce_subset_with_equalities(i, j, info);
2575 if (change != isl_change_none)
2576 return change;
2578 change = coalesce_subset_with_equalities(j, i, info);
2579 if (change != isl_change_none)
2580 return invert_change(change);
2582 return isl_change_none;
2585 /* Does "bmap" involve any divs that themselves refer to divs?
2587 static int has_nested_div(__isl_keep isl_basic_map *bmap)
2589 int i;
2590 unsigned total;
2591 unsigned n_div;
2593 total = isl_basic_map_dim(bmap, isl_dim_all);
2594 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2595 total -= n_div;
2597 for (i = 0; i < n_div; ++i)
2598 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
2599 n_div) != -1)
2600 return 1;
2602 return 0;
2605 /* Return a list of affine expressions, one for each integer division
2606 * in "bmap_i". For each integer division that also appears in "bmap_j",
2607 * the affine expression is set to NaN. The number of NaNs in the list
2608 * is equal to the number of integer divisions in "bmap_j".
2609 * For the other integer divisions of "bmap_i", the corresponding
2610 * element in the list is a purely affine expression equal to the integer
2611 * division in "hull".
2612 * If no such list can be constructed, then the number of elements
2613 * in the returned list is smaller than the number of integer divisions
2614 * in "bmap_i".
2616 static __isl_give isl_aff_list *set_up_substitutions(
2617 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
2618 __isl_take isl_basic_map *hull)
2620 unsigned n_div_i, n_div_j, total;
2621 isl_ctx *ctx;
2622 isl_local_space *ls;
2623 isl_basic_set *wrap_hull;
2624 isl_aff *aff_nan;
2625 isl_aff_list *list;
2626 int i, j;
2628 if (!hull)
2629 return NULL;
2631 ctx = isl_basic_map_get_ctx(hull);
2633 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
2634 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
2635 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
2637 ls = isl_basic_map_get_local_space(bmap_i);
2638 ls = isl_local_space_wrap(ls);
2639 wrap_hull = isl_basic_map_wrap(hull);
2641 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
2642 list = isl_aff_list_alloc(ctx, n_div_i);
2644 j = 0;
2645 for (i = 0; i < n_div_i; ++i) {
2646 isl_aff *aff;
2648 if (j < n_div_j &&
2649 isl_seq_eq(bmap_i->div[i], bmap_j->div[j], 2 + total)) {
2650 ++j;
2651 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
2652 continue;
2654 if (n_div_i - i <= n_div_j - j)
2655 break;
2657 aff = isl_local_space_get_div(ls, i);
2658 aff = isl_aff_substitute_equalities(aff,
2659 isl_basic_set_copy(wrap_hull));
2660 aff = isl_aff_floor(aff);
2661 if (!aff)
2662 goto error;
2663 if (isl_aff_dim(aff, isl_dim_div) != 0) {
2664 isl_aff_free(aff);
2665 break;
2668 list = isl_aff_list_add(list, aff);
2671 isl_aff_free(aff_nan);
2672 isl_local_space_free(ls);
2673 isl_basic_set_free(wrap_hull);
2675 return list;
2676 error:
2677 isl_aff_free(aff_nan);
2678 isl_local_space_free(ls);
2679 isl_basic_set_free(wrap_hull);
2680 isl_aff_list_free(list);
2681 return NULL;
2684 /* Add variables to info->bmap and info->tab corresponding to the elements
2685 * in "list" that are not set to NaN.
2686 * "extra_var" is the number of these elements.
2687 * "dim" is the offset in the variables of "tab" where we should
2688 * start considering the elements in "list".
2689 * When this function returns, the total number of variables in "tab"
2690 * is equal to "dim" plus the number of elements in "list".
2692 * The newly added existentially quantified variables are not given
2693 * an explicit representation because the corresponding div constraints
2694 * do not appear in info->bmap. These constraints are not added
2695 * to info->bmap because for internal consistency, they would need to
2696 * be added to info->tab as well, where they could combine with the equality
2697 * that is added later to result in constraints that do not hold
2698 * in the original input.
2700 static int add_sub_vars(struct isl_coalesce_info *info,
2701 __isl_keep isl_aff_list *list, int dim, int extra_var)
2703 int i, j, n, d;
2704 isl_space *space;
2706 space = isl_basic_map_get_space(info->bmap);
2707 info->bmap = isl_basic_map_cow(info->bmap);
2708 info->bmap = isl_basic_map_extend_space(info->bmap, space,
2709 extra_var, 0, 0);
2710 if (!info->bmap)
2711 return -1;
2712 n = isl_aff_list_n_aff(list);
2713 for (i = 0; i < n; ++i) {
2714 int is_nan;
2715 isl_aff *aff;
2717 aff = isl_aff_list_get_aff(list, i);
2718 is_nan = isl_aff_is_nan(aff);
2719 isl_aff_free(aff);
2720 if (is_nan < 0)
2721 return -1;
2722 if (is_nan)
2723 continue;
2725 if (isl_tab_insert_var(info->tab, dim + i) < 0)
2726 return -1;
2727 d = isl_basic_map_alloc_div(info->bmap);
2728 if (d < 0)
2729 return -1;
2730 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
2731 if (!info->bmap)
2732 return -1;
2733 for (j = d; j > i; --j)
2734 isl_basic_map_swap_div(info->bmap, j - 1, j);
2737 return 0;
2740 /* For each element in "list" that is not set to NaN, fix the corresponding
2741 * variable in "tab" to the purely affine expression defined by the element.
2742 * "dim" is the offset in the variables of "tab" where we should
2743 * start considering the elements in "list".
2745 * This function assumes that a sufficient number of rows and
2746 * elements in the constraint array are available in the tableau.
2748 static int add_sub_equalities(struct isl_tab *tab,
2749 __isl_keep isl_aff_list *list, int dim)
2751 int i, n;
2752 isl_ctx *ctx;
2753 isl_vec *sub;
2754 isl_aff *aff;
2756 n = isl_aff_list_n_aff(list);
2758 ctx = isl_tab_get_ctx(tab);
2759 sub = isl_vec_alloc(ctx, 1 + dim + n);
2760 if (!sub)
2761 return -1;
2762 isl_seq_clr(sub->el + 1 + dim, n);
2764 for (i = 0; i < n; ++i) {
2765 aff = isl_aff_list_get_aff(list, i);
2766 if (!aff)
2767 goto error;
2768 if (isl_aff_is_nan(aff)) {
2769 isl_aff_free(aff);
2770 continue;
2772 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
2773 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
2774 if (isl_tab_add_eq(tab, sub->el) < 0)
2775 goto error;
2776 isl_int_set_si(sub->el[1 + dim + i], 0);
2777 isl_aff_free(aff);
2780 isl_vec_free(sub);
2781 return 0;
2782 error:
2783 isl_aff_free(aff);
2784 isl_vec_free(sub);
2785 return -1;
2788 /* Add variables to info->tab and info->bmap corresponding to the elements
2789 * in "list" that are not set to NaN. The value of the added variable
2790 * in info->tab is fixed to the purely affine expression defined by the element.
2791 * "dim" is the offset in the variables of info->tab where we should
2792 * start considering the elements in "list".
2793 * When this function returns, the total number of variables in info->tab
2794 * is equal to "dim" plus the number of elements in "list".
2796 static int add_subs(struct isl_coalesce_info *info,
2797 __isl_keep isl_aff_list *list, int dim)
2799 int extra_var;
2800 int n;
2802 if (!list)
2803 return -1;
2805 n = isl_aff_list_n_aff(list);
2806 extra_var = n - (info->tab->n_var - dim);
2808 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
2809 return -1;
2810 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
2811 return -1;
2812 if (add_sub_vars(info, list, dim, extra_var) < 0)
2813 return -1;
2815 return add_sub_equalities(info->tab, list, dim);
2818 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
2819 * divisions in "i" but not in "j" to basic map "j", with values
2820 * specified by "list". The total number of elements in "list"
2821 * is equal to the number of integer divisions in "i", while the number
2822 * of NaN elements in the list is equal to the number of integer divisions
2823 * in "j".
2825 * If no coalescing can be performed, then we need to revert basic map "j"
2826 * to its original state. We do the same if basic map "i" gets dropped
2827 * during the coalescing, even though this should not happen in practice
2828 * since we have already checked for "j" being a subset of "i"
2829 * before we reach this stage.
2831 static enum isl_change coalesce_with_subs(int i, int j,
2832 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
2834 isl_basic_map *bmap_j;
2835 struct isl_tab_undo *snap;
2836 unsigned dim;
2837 enum isl_change change;
2839 bmap_j = isl_basic_map_copy(info[j].bmap);
2840 snap = isl_tab_snap(info[j].tab);
2842 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
2843 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
2844 if (add_subs(&info[j], list, dim) < 0)
2845 goto error;
2847 change = coalesce_local_pair(i, j, info);
2848 if (change != isl_change_none && change != isl_change_drop_first) {
2849 isl_basic_map_free(bmap_j);
2850 } else {
2851 isl_basic_map_free(info[j].bmap);
2852 info[j].bmap = bmap_j;
2854 if (isl_tab_rollback(info[j].tab, snap) < 0)
2855 return isl_change_error;
2858 return change;
2859 error:
2860 isl_basic_map_free(bmap_j);
2861 return isl_change_error;
2864 /* Check if we can coalesce basic map "j" into basic map "i" after copying
2865 * those extra integer divisions in "i" that can be simplified away
2866 * using the extra equalities in "j".
2867 * All divs are assumed to be known and not contain any nested divs.
2869 * We first check if there are any extra equalities in "j" that we
2870 * can exploit. Then we check if every integer division in "i"
2871 * either already appears in "j" or can be simplified using the
2872 * extra equalities to a purely affine expression.
2873 * If these tests succeed, then we try to coalesce the two basic maps
2874 * by introducing extra dimensions in "j" corresponding to
2875 * the extra integer divsisions "i" fixed to the corresponding
2876 * purely affine expression.
2878 static enum isl_change check_coalesce_into_eq(int i, int j,
2879 struct isl_coalesce_info *info)
2881 unsigned n_div_i, n_div_j;
2882 isl_basic_map *hull_i, *hull_j;
2883 int equal, empty;
2884 isl_aff_list *list;
2885 enum isl_change change;
2887 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
2888 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
2889 if (n_div_i <= n_div_j)
2890 return isl_change_none;
2891 if (info[j].bmap->n_eq == 0)
2892 return isl_change_none;
2894 hull_i = isl_basic_map_copy(info[i].bmap);
2895 hull_i = isl_basic_map_plain_affine_hull(hull_i);
2896 hull_j = isl_basic_map_copy(info[j].bmap);
2897 hull_j = isl_basic_map_plain_affine_hull(hull_j);
2899 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
2900 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
2901 empty = isl_basic_map_plain_is_empty(hull_j);
2902 isl_basic_map_free(hull_i);
2904 if (equal < 0 || empty < 0)
2905 goto error;
2906 if (equal || empty) {
2907 isl_basic_map_free(hull_j);
2908 return isl_change_none;
2911 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
2912 if (!list)
2913 return isl_change_error;
2914 if (isl_aff_list_n_aff(list) < n_div_i)
2915 change = isl_change_none;
2916 else
2917 change = coalesce_with_subs(i, j, info, list);
2919 isl_aff_list_free(list);
2921 return change;
2922 error:
2923 isl_basic_map_free(hull_j);
2924 return isl_change_error;
2927 /* Check if we can coalesce basic maps "i" and "j" after copying
2928 * those extra integer divisions in one of the basic maps that can
2929 * be simplified away using the extra equalities in the other basic map.
2930 * We require all divs to be known in both basic maps.
2931 * Furthermore, to simplify the comparison of div expressions,
2932 * we do not allow any nested integer divisions.
2934 static enum isl_change check_coalesce_eq(int i, int j,
2935 struct isl_coalesce_info *info)
2937 int known, nested;
2938 enum isl_change change;
2940 known = isl_basic_map_divs_known(info[i].bmap);
2941 if (known < 0 || !known)
2942 return known < 0 ? isl_change_error : isl_change_none;
2943 known = isl_basic_map_divs_known(info[j].bmap);
2944 if (known < 0 || !known)
2945 return known < 0 ? isl_change_error : isl_change_none;
2946 nested = has_nested_div(info[i].bmap);
2947 if (nested < 0 || nested)
2948 return nested < 0 ? isl_change_error : isl_change_none;
2949 nested = has_nested_div(info[j].bmap);
2950 if (nested < 0 || nested)
2951 return nested < 0 ? isl_change_error : isl_change_none;
2953 change = check_coalesce_into_eq(i, j, info);
2954 if (change != isl_change_none)
2955 return change;
2956 change = check_coalesce_into_eq(j, i, info);
2957 if (change != isl_change_none)
2958 return invert_change(change);
2960 return isl_change_none;
2963 /* Check if the union of the given pair of basic maps
2964 * can be represented by a single basic map.
2965 * If so, replace the pair by the single basic map and return
2966 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2967 * Otherwise, return isl_change_none.
2969 * We first check if the two basic maps live in the same local space,
2970 * after aligning the divs that differ by only an integer constant.
2971 * If so, we do the complete check. Otherwise, we check if they have
2972 * the same number of integer divisions and can be coalesced, if one is
2973 * an obvious subset of the other or if the extra integer divisions
2974 * of one basic map can be simplified away using the extra equalities
2975 * of the other basic map.
2977 static enum isl_change coalesce_pair(int i, int j,
2978 struct isl_coalesce_info *info)
2980 int same;
2981 enum isl_change change;
2983 if (harmonize_divs(&info[i], &info[j]) < 0)
2984 return isl_change_error;
2985 same = same_divs(info[i].bmap, info[j].bmap);
2986 if (same < 0)
2987 return isl_change_error;
2988 if (same)
2989 return coalesce_local_pair(i, j, info);
2991 if (info[i].bmap->n_div == info[j].bmap->n_div) {
2992 change = coalesce_local_pair(i, j, info);
2993 if (change != isl_change_none)
2994 return change;
2997 change = coalesce_divs(i, j, info);
2998 if (change != isl_change_none)
2999 return change;
3001 return check_coalesce_eq(i, j, info);
3004 /* Return the maximum of "a" and "b".
3006 static int isl_max(int a, int b)
3008 return a > b ? a : b;
3011 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3012 * with those in the range [start2, end2[, skipping basic maps
3013 * that have been removed (either before or within this function).
3015 * For each basic map i in the first range, we check if it can be coalesced
3016 * with respect to any previously considered basic map j in the second range.
3017 * If i gets dropped (because it was a subset of some j), then
3018 * we can move on to the next basic map.
3019 * If j gets dropped, we need to continue checking against the other
3020 * previously considered basic maps.
3021 * If the two basic maps got fused, then we recheck the fused basic map
3022 * against the previously considered basic maps, starting at i + 1
3023 * (even if start2 is greater than i + 1).
3025 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3026 int start1, int end1, int start2, int end2)
3028 int i, j;
3030 for (i = end1 - 1; i >= start1; --i) {
3031 if (info[i].removed)
3032 continue;
3033 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3034 enum isl_change changed;
3036 if (info[j].removed)
3037 continue;
3038 if (info[i].removed)
3039 isl_die(ctx, isl_error_internal,
3040 "basic map unexpectedly removed",
3041 return -1);
3042 changed = coalesce_pair(i, j, info);
3043 switch (changed) {
3044 case isl_change_error:
3045 return -1;
3046 case isl_change_none:
3047 case isl_change_drop_second:
3048 continue;
3049 case isl_change_drop_first:
3050 j = end2;
3051 break;
3052 case isl_change_fuse:
3053 j = i;
3054 break;
3059 return 0;
3062 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3064 * We consider groups of basic maps that live in the same apparent
3065 * affine hull and we first coalesce within such a group before we
3066 * coalesce the elements in the group with elements of previously
3067 * considered groups. If a fuse happens during the second phase,
3068 * then we also reconsider the elements within the group.
3070 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3072 int start, end;
3074 for (end = n; end > 0; end = start) {
3075 start = end - 1;
3076 while (start >= 1 &&
3077 info[start - 1].hull_hash == info[start].hull_hash)
3078 start--;
3079 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3080 return -1;
3081 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3082 return -1;
3085 return 0;
3088 /* Update the basic maps in "map" based on the information in "info".
3089 * In particular, remove the basic maps that have been marked removed and
3090 * update the others based on the information in the corresponding tableau.
3091 * Since we detected implicit equalities without calling
3092 * isl_basic_map_gauss, we need to do it now.
3093 * Also call isl_basic_map_simplify if we may have lost the definition
3094 * of one or more integer divisions.
3096 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3097 int n, struct isl_coalesce_info *info)
3099 int i;
3101 if (!map)
3102 return NULL;
3104 for (i = n - 1; i >= 0; --i) {
3105 if (info[i].removed) {
3106 isl_basic_map_free(map->p[i]);
3107 if (i != map->n - 1)
3108 map->p[i] = map->p[map->n - 1];
3109 map->n--;
3110 continue;
3113 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3114 info[i].tab);
3115 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3116 if (info[i].simplify)
3117 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3118 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3119 if (!info[i].bmap)
3120 return isl_map_free(map);
3121 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3122 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3123 isl_basic_map_free(map->p[i]);
3124 map->p[i] = info[i].bmap;
3125 info[i].bmap = NULL;
3128 return map;
3131 /* For each pair of basic maps in the map, check if the union of the two
3132 * can be represented by a single basic map.
3133 * If so, replace the pair by the single basic map and start over.
3135 * We factor out any (hidden) common factor from the constraint
3136 * coefficients to improve the detection of adjacent constraints.
3138 * Since we are constructing the tableaus of the basic maps anyway,
3139 * we exploit them to detect implicit equalities and redundant constraints.
3140 * This also helps the coalescing as it can ignore the redundant constraints.
3141 * In order to avoid confusion, we make all implicit equalities explicit
3142 * in the basic maps. We don't call isl_basic_map_gauss, though,
3143 * as that may affect the number of constraints.
3144 * This means that we have to call isl_basic_map_gauss at the end
3145 * of the computation (in update_basic_maps) to ensure that
3146 * the basic maps are not left in an unexpected state.
3147 * For each basic map, we also compute the hash of the apparent affine hull
3148 * for use in coalesce.
3150 struct isl_map *isl_map_coalesce(struct isl_map *map)
3152 int i;
3153 unsigned n;
3154 isl_ctx *ctx;
3155 struct isl_coalesce_info *info = NULL;
3157 map = isl_map_remove_empty_parts(map);
3158 if (!map)
3159 return NULL;
3161 if (map->n <= 1)
3162 return map;
3164 ctx = isl_map_get_ctx(map);
3165 map = isl_map_sort_divs(map);
3166 map = isl_map_cow(map);
3168 if (!map)
3169 return NULL;
3171 n = map->n;
3173 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3174 if (!info)
3175 goto error;
3177 for (i = 0; i < map->n; ++i) {
3178 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3179 if (!map->p[i])
3180 goto error;
3181 info[i].bmap = isl_basic_map_copy(map->p[i]);
3182 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3183 if (!info[i].tab)
3184 goto error;
3185 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3186 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3187 goto error;
3188 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3189 info[i].bmap);
3190 if (!info[i].bmap)
3191 goto error;
3192 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3193 if (isl_tab_detect_redundant(info[i].tab) < 0)
3194 goto error;
3195 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3196 goto error;
3198 for (i = map->n - 1; i >= 0; --i)
3199 if (info[i].tab->empty)
3200 drop(&info[i]);
3202 if (coalesce(ctx, n, info) < 0)
3203 goto error;
3205 map = update_basic_maps(map, n, info);
3207 clear_coalesce_info(n, info);
3209 return map;
3210 error:
3211 clear_coalesce_info(n, info);
3212 isl_map_free(map);
3213 return NULL;
3216 /* For each pair of basic sets in the set, check if the union of the two
3217 * can be represented by a single basic set.
3218 * If so, replace the pair by the single basic set and start over.
3220 struct isl_set *isl_set_coalesce(struct isl_set *set)
3222 return set_from_map(isl_map_coalesce(set_to_map(set)));