isl_coalesce.c: extract out shared any_eq and any_ineq
[isl.git] / isl_coalesce.c
blob4200c6baa11e44f5df0eeb3810e357839e761296
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
18 * CS 42112, 75589 Paris Cedex 12, France
21 #include <isl_ctx_private.h>
22 #include "isl_map_private.h"
23 #include <isl_seq.h>
24 #include <isl/options.h>
25 #include "isl_tab.h"
26 #include <isl_mat_private.h>
27 #include <isl_local_space_private.h>
28 #include <isl_val_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_aff_private.h>
31 #include <isl_equalities.h>
32 #include <isl_constraint_private.h>
34 #include <set_to_map.c>
35 #include <set_from_map.c>
37 #define STATUS_ERROR -1
38 #define STATUS_REDUNDANT 1
39 #define STATUS_VALID 2
40 #define STATUS_SEPARATE 3
41 #define STATUS_CUT 4
42 #define STATUS_ADJ_EQ 5
43 #define STATUS_ADJ_INEQ 6
45 static int status_in(isl_int *ineq, struct isl_tab *tab)
47 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
48 switch (type) {
49 default:
50 case isl_ineq_error: return STATUS_ERROR;
51 case isl_ineq_redundant: return STATUS_VALID;
52 case isl_ineq_separate: return STATUS_SEPARATE;
53 case isl_ineq_cut: return STATUS_CUT;
54 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
55 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
59 /* Compute the position of the equalities of basic map "bmap_i"
60 * with respect to the basic map represented by "tab_j".
61 * The resulting array has twice as many entries as the number
62 * of equalities corresponding to the two inequalities to which
63 * each equality corresponds.
65 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
66 struct isl_tab *tab_j)
68 int k, l;
69 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
70 unsigned dim;
72 if (!eq)
73 return NULL;
75 dim = isl_basic_map_total_dim(bmap_i);
76 for (k = 0; k < bmap_i->n_eq; ++k) {
77 for (l = 0; l < 2; ++l) {
78 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
79 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
80 if (eq[2 * k + l] == STATUS_ERROR)
81 goto error;
85 return eq;
86 error:
87 free(eq);
88 return NULL;
91 /* Compute the position of the inequalities of basic map "bmap_i"
92 * (also represented by "tab_i", if not NULL) with respect to the basic map
93 * represented by "tab_j".
95 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
96 struct isl_tab *tab_i, struct isl_tab *tab_j)
98 int k;
99 unsigned n_eq = bmap_i->n_eq;
100 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
102 if (!ineq)
103 return NULL;
105 for (k = 0; k < bmap_i->n_ineq; ++k) {
106 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
107 ineq[k] = STATUS_REDUNDANT;
108 continue;
110 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
111 if (ineq[k] == STATUS_ERROR)
112 goto error;
113 if (ineq[k] == STATUS_SEPARATE)
114 break;
117 return ineq;
118 error:
119 free(ineq);
120 return NULL;
123 static int any(int *con, unsigned len, int status)
125 int i;
127 for (i = 0; i < len ; ++i)
128 if (con[i] == status)
129 return 1;
130 return 0;
133 /* Return the first position of "status" in the list "con" of length "len".
134 * Return -1 if there is no such entry.
136 static int find(int *con, unsigned len, int status)
138 int i;
140 for (i = 0; i < len ; ++i)
141 if (con[i] == status)
142 return i;
143 return -1;
146 static int count(int *con, unsigned len, int status)
148 int i;
149 int c = 0;
151 for (i = 0; i < len ; ++i)
152 if (con[i] == status)
153 c++;
154 return c;
157 static int all(int *con, unsigned len, int status)
159 int i;
161 for (i = 0; i < len ; ++i) {
162 if (con[i] == STATUS_REDUNDANT)
163 continue;
164 if (con[i] != status)
165 return 0;
167 return 1;
170 /* Internal information associated to a basic map in a map
171 * that is to be coalesced by isl_map_coalesce.
173 * "bmap" is the basic map itself (or NULL if "removed" is set)
174 * "tab" is the corresponding tableau (or NULL if "removed" is set)
175 * "hull_hash" identifies the affine space in which "bmap" lives.
176 * "removed" is set if this basic map has been removed from the map
177 * "simplify" is set if this basic map may have some unknown integer
178 * divisions that were not present in the input basic maps. The basic
179 * map should then be simplified such that we may be able to find
180 * a definition among the constraints.
182 * "eq" and "ineq" are only set if we are currently trying to coalesce
183 * this basic map with another basic map, in which case they represent
184 * the position of the inequalities of this basic map with respect to
185 * the other basic map. The number of elements in the "eq" array
186 * is twice the number of equalities in the "bmap", corresponding
187 * to the two inequalities that make up each equality.
189 struct isl_coalesce_info {
190 isl_basic_map *bmap;
191 struct isl_tab *tab;
192 uint32_t hull_hash;
193 int removed;
194 int simplify;
195 int *eq;
196 int *ineq;
199 /* Is there any (half of an) equality constraint in the description
200 * of the basic map represented by "info" that
201 * has position "status" with respect to the other basic map?
203 static int any_eq(struct isl_coalesce_info *info, int status)
205 unsigned n_eq;
207 n_eq = isl_basic_map_n_equality(info->bmap);
208 return any(info->eq, 2 * n_eq, status);
211 /* Is there any inequality constraint in the description
212 * of the basic map represented by "info" that
213 * has position "status" with respect to the other basic map?
215 static int any_ineq(struct isl_coalesce_info *info, int status)
217 unsigned n_ineq;
219 n_ineq = isl_basic_map_n_inequality(info->bmap);
220 return any(info->ineq, n_ineq, status);
223 /* Are all non-redundant constraints of the basic map represented by "info"
224 * either valid or cut constraints with respect to the other basic map?
226 static int all_valid_or_cut(struct isl_coalesce_info *info)
228 int i;
230 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
231 if (info->eq[i] == STATUS_REDUNDANT)
232 continue;
233 if (info->eq[i] == STATUS_VALID)
234 continue;
235 if (info->eq[i] == STATUS_CUT)
236 continue;
237 return 0;
240 for (i = 0; i < info->bmap->n_ineq; ++i) {
241 if (info->ineq[i] == STATUS_REDUNDANT)
242 continue;
243 if (info->ineq[i] == STATUS_VALID)
244 continue;
245 if (info->ineq[i] == STATUS_CUT)
246 continue;
247 return 0;
250 return 1;
253 /* Compute the hash of the (apparent) affine hull of info->bmap (with
254 * the existentially quantified variables removed) and store it
255 * in info->hash.
257 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
259 isl_basic_map *hull;
260 unsigned n_div;
262 hull = isl_basic_map_copy(info->bmap);
263 hull = isl_basic_map_plain_affine_hull(hull);
264 n_div = isl_basic_map_dim(hull, isl_dim_div);
265 hull = isl_basic_map_drop_constraints_involving_dims(hull,
266 isl_dim_div, 0, n_div);
267 info->hull_hash = isl_basic_map_get_hash(hull);
268 isl_basic_map_free(hull);
270 return hull ? 0 : -1;
273 /* Free all the allocated memory in an array
274 * of "n" isl_coalesce_info elements.
276 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
278 int i;
280 if (!info)
281 return;
283 for (i = 0; i < n; ++i) {
284 isl_basic_map_free(info[i].bmap);
285 isl_tab_free(info[i].tab);
288 free(info);
291 /* Drop the basic map represented by "info".
292 * That is, clear the memory associated to the entry and
293 * mark it as having been removed.
295 static void drop(struct isl_coalesce_info *info)
297 info->bmap = isl_basic_map_free(info->bmap);
298 isl_tab_free(info->tab);
299 info->tab = NULL;
300 info->removed = 1;
303 /* Exchange the information in "info1" with that in "info2".
305 static void exchange(struct isl_coalesce_info *info1,
306 struct isl_coalesce_info *info2)
308 struct isl_coalesce_info info;
310 info = *info1;
311 *info1 = *info2;
312 *info2 = info;
315 /* This type represents the kind of change that has been performed
316 * while trying to coalesce two basic maps.
318 * isl_change_none: nothing was changed
319 * isl_change_drop_first: the first basic map was removed
320 * isl_change_drop_second: the second basic map was removed
321 * isl_change_fuse: the two basic maps were replaced by a new basic map.
323 enum isl_change {
324 isl_change_error = -1,
325 isl_change_none = 0,
326 isl_change_drop_first,
327 isl_change_drop_second,
328 isl_change_fuse,
331 /* Update "change" based on an interchange of the first and the second
332 * basic map. That is, interchange isl_change_drop_first and
333 * isl_change_drop_second.
335 static enum isl_change invert_change(enum isl_change change)
337 switch (change) {
338 case isl_change_error:
339 return isl_change_error;
340 case isl_change_none:
341 return isl_change_none;
342 case isl_change_drop_first:
343 return isl_change_drop_second;
344 case isl_change_drop_second:
345 return isl_change_drop_first;
346 case isl_change_fuse:
347 return isl_change_fuse;
350 return isl_change_error;
353 /* Add the valid constraints of the basic map represented by "info"
354 * to "bmap". "len" is the size of the constraints.
355 * If only one of the pair of inequalities that make up an equality
356 * is valid, then add that inequality.
358 static __isl_give isl_basic_map *add_valid_constraints(
359 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
360 unsigned len)
362 int k, l;
364 if (!bmap)
365 return NULL;
367 for (k = 0; k < info->bmap->n_eq; ++k) {
368 if (info->eq[2 * k] == STATUS_VALID &&
369 info->eq[2 * k + 1] == STATUS_VALID) {
370 l = isl_basic_map_alloc_equality(bmap);
371 if (l < 0)
372 return isl_basic_map_free(bmap);
373 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
374 } else if (info->eq[2 * k] == STATUS_VALID) {
375 l = isl_basic_map_alloc_inequality(bmap);
376 if (l < 0)
377 return isl_basic_map_free(bmap);
378 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
379 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
380 l = isl_basic_map_alloc_inequality(bmap);
381 if (l < 0)
382 return isl_basic_map_free(bmap);
383 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
387 for (k = 0; k < info->bmap->n_ineq; ++k) {
388 if (info->ineq[k] != STATUS_VALID)
389 continue;
390 l = isl_basic_map_alloc_inequality(bmap);
391 if (l < 0)
392 return isl_basic_map_free(bmap);
393 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
396 return bmap;
399 /* Is "bmap" defined by a number of (non-redundant) constraints that
400 * is greater than the number of constraints of basic maps i and j combined?
401 * Equalities are counted as two inequalities.
403 static int number_of_constraints_increases(int i, int j,
404 struct isl_coalesce_info *info,
405 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
407 int k, n_old, n_new;
409 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
410 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
412 n_new = 2 * bmap->n_eq;
413 for (k = 0; k < bmap->n_ineq; ++k)
414 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
415 ++n_new;
417 return n_new > n_old;
420 /* Replace the pair of basic maps i and j by the basic map bounded
421 * by the valid constraints in both basic maps and the constraints
422 * in extra (if not NULL).
423 * Place the fused basic map in the position that is the smallest of i and j.
425 * If "detect_equalities" is set, then look for equalities encoded
426 * as pairs of inequalities.
427 * If "check_number" is set, then the original basic maps are only
428 * replaced if the total number of constraints does not increase.
429 * While the number of integer divisions in the two basic maps
430 * is assumed to be the same, the actual definitions may be different.
431 * We only copy the definition from one of the basic map if it is
432 * the same as that of the other basic map. Otherwise, we mark
433 * the integer division as unknown and simplify the basic map
434 * in an attempt to recover the integer division definition.
436 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
437 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
439 int k, l;
440 struct isl_basic_map *fused = NULL;
441 struct isl_tab *fused_tab = NULL;
442 unsigned total = isl_basic_map_total_dim(info[i].bmap);
443 unsigned extra_rows = extra ? extra->n_row : 0;
444 unsigned n_eq, n_ineq;
445 int simplify = 0;
447 if (j < i)
448 return fuse(j, i, info, extra, detect_equalities, check_number);
450 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
451 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
452 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
453 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
454 fused = add_valid_constraints(fused, &info[i], 1 + total);
455 fused = add_valid_constraints(fused, &info[j], 1 + total);
456 if (!fused)
457 goto error;
458 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
459 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
460 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
462 for (k = 0; k < info[i].bmap->n_div; ++k) {
463 int l = isl_basic_map_alloc_div(fused);
464 if (l < 0)
465 goto error;
466 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
467 1 + 1 + total)) {
468 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
469 1 + 1 + total);
470 } else {
471 isl_int_set_si(fused->div[l][0], 0);
472 simplify = 1;
476 for (k = 0; k < extra_rows; ++k) {
477 l = isl_basic_map_alloc_inequality(fused);
478 if (l < 0)
479 goto error;
480 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
483 if (detect_equalities)
484 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
485 fused = isl_basic_map_gauss(fused, NULL);
486 if (simplify || info[j].simplify) {
487 fused = isl_basic_map_simplify(fused);
488 info[i].simplify = 0;
490 fused = isl_basic_map_finalize(fused);
492 fused_tab = isl_tab_from_basic_map(fused, 0);
493 if (isl_tab_detect_redundant(fused_tab) < 0)
494 goto error;
496 if (check_number &&
497 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
498 isl_tab_free(fused_tab);
499 isl_basic_map_free(fused);
500 return isl_change_none;
503 isl_basic_map_free(info[i].bmap);
504 info[i].bmap = fused;
505 isl_tab_free(info[i].tab);
506 info[i].tab = fused_tab;
507 drop(&info[j]);
509 return isl_change_fuse;
510 error:
511 isl_tab_free(fused_tab);
512 isl_basic_map_free(fused);
513 return isl_change_error;
516 /* Given a pair of basic maps i and j such that all constraints are either
517 * "valid" or "cut", check if the facets corresponding to the "cut"
518 * constraints of i lie entirely within basic map j.
519 * If so, replace the pair by the basic map consisting of the valid
520 * constraints in both basic maps.
521 * Checking whether the facet lies entirely within basic map j
522 * is performed by checking whether the constraints of basic map j
523 * are valid for the facet. These tests are performed on a rational
524 * tableau to avoid the theoretical possibility that a constraint
525 * that was considered to be a cut constraint for the entire basic map i
526 * happens to be considered to be a valid constraint for the facet,
527 * even though it cuts off the same rational points.
529 * To see that we are not introducing any extra points, call the
530 * two basic maps A and B and the resulting map U and let x
531 * be an element of U \setminus ( A \cup B ).
532 * A line connecting x with an element of A \cup B meets a facet F
533 * of either A or B. Assume it is a facet of B and let c_1 be
534 * the corresponding facet constraint. We have c_1(x) < 0 and
535 * so c_1 is a cut constraint. This implies that there is some
536 * (possibly rational) point x' satisfying the constraints of A
537 * and the opposite of c_1 as otherwise c_1 would have been marked
538 * valid for A. The line connecting x and x' meets a facet of A
539 * in a (possibly rational) point that also violates c_1, but this
540 * is impossible since all cut constraints of B are valid for all
541 * cut facets of A.
542 * In case F is a facet of A rather than B, then we can apply the
543 * above reasoning to find a facet of B separating x from A \cup B first.
545 static enum isl_change check_facets(int i, int j,
546 struct isl_coalesce_info *info)
548 int k, l;
549 struct isl_tab_undo *snap, *snap2;
550 unsigned n_eq = info[i].bmap->n_eq;
552 snap = isl_tab_snap(info[i].tab);
553 if (isl_tab_mark_rational(info[i].tab) < 0)
554 return isl_change_error;
555 snap2 = isl_tab_snap(info[i].tab);
557 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
558 if (info[i].ineq[k] != STATUS_CUT)
559 continue;
560 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
561 return isl_change_error;
562 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
563 int stat;
564 if (info[j].ineq[l] != STATUS_CUT)
565 continue;
566 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
567 if (stat < 0)
568 return isl_change_error;
569 if (stat != STATUS_VALID)
570 break;
572 if (isl_tab_rollback(info[i].tab, snap2) < 0)
573 return isl_change_error;
574 if (l < info[j].bmap->n_ineq)
575 break;
578 if (k < info[i].bmap->n_ineq) {
579 if (isl_tab_rollback(info[i].tab, snap) < 0)
580 return isl_change_error;
581 return isl_change_none;
583 return fuse(i, j, info, NULL, 0, 0);
586 /* Check if info->bmap contains the basic map represented
587 * by the tableau "tab".
588 * For each equality, we check both the constraint itself
589 * (as an inequality) and its negation. Make sure the
590 * equality is returned to its original state before returning.
592 static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
594 int k;
595 unsigned dim;
596 isl_basic_map *bmap = info->bmap;
598 dim = isl_basic_map_total_dim(bmap);
599 for (k = 0; k < bmap->n_eq; ++k) {
600 int stat;
601 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
602 stat = status_in(bmap->eq[k], tab);
603 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
604 if (stat < 0)
605 return isl_bool_error;
606 if (stat != STATUS_VALID)
607 return isl_bool_false;
608 stat = status_in(bmap->eq[k], tab);
609 if (stat < 0)
610 return isl_bool_error;
611 if (stat != STATUS_VALID)
612 return isl_bool_false;
615 for (k = 0; k < bmap->n_ineq; ++k) {
616 int stat;
617 if (info->ineq[k] == STATUS_REDUNDANT)
618 continue;
619 stat = status_in(bmap->ineq[k], tab);
620 if (stat < 0)
621 return isl_bool_error;
622 if (stat != STATUS_VALID)
623 return isl_bool_false;
625 return isl_bool_true;
628 /* Basic map "i" has an inequality (say "k") that is adjacent
629 * to some inequality of basic map "j". All the other inequalities
630 * are valid for "j".
631 * Check if basic map "j" forms an extension of basic map "i".
633 * Note that this function is only called if some of the equalities or
634 * inequalities of basic map "j" do cut basic map "i". The function is
635 * correct even if there are no such cut constraints, but in that case
636 * the additional checks performed by this function are overkill.
638 * In particular, we replace constraint k, say f >= 0, by constraint
639 * f <= -1, add the inequalities of "j" that are valid for "i"
640 * and check if the result is a subset of basic map "j".
641 * To improve the chances of the subset relation being detected,
642 * any variable that only attains a single integer value
643 * in the tableau of "i" is first fixed to that value.
644 * If the result is a subset, then we know that this result is exactly equal
645 * to basic map "j" since all its constraints are valid for basic map "j".
646 * By combining the valid constraints of "i" (all equalities and all
647 * inequalities except "k") and the valid constraints of "j" we therefore
648 * obtain a basic map that is equal to their union.
649 * In this case, there is no need to perform a rollback of the tableau
650 * since it is going to be destroyed in fuse().
653 * |\__ |\__
654 * | \__ | \__
655 * | \_ => | \__
656 * |_______| _ |_________\
659 * |\ |\
660 * | \ | \
661 * | \ | \
662 * | | | \
663 * | ||\ => | \
664 * | || \ | \
665 * | || | | |
666 * |__||_/ |_____/
668 static enum isl_change is_adj_ineq_extension(int i, int j,
669 struct isl_coalesce_info *info)
671 int k;
672 struct isl_tab_undo *snap;
673 unsigned n_eq = info[i].bmap->n_eq;
674 unsigned total = isl_basic_map_total_dim(info[i].bmap);
675 isl_stat r;
676 isl_bool super;
678 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
679 return isl_change_error;
681 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
682 if (k < 0)
683 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
684 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
685 return isl_change_error);
687 snap = isl_tab_snap(info[i].tab);
689 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
690 return isl_change_error;
692 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
693 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
694 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
695 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
696 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
697 if (r < 0)
698 return isl_change_error;
700 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
701 if (info[j].ineq[k] != STATUS_VALID)
702 continue;
703 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
704 return isl_change_error;
706 if (isl_tab_detect_constants(info[i].tab) < 0)
707 return isl_change_error;
709 super = contains(&info[j], info[i].tab);
710 if (super < 0)
711 return isl_change_error;
712 if (super)
713 return fuse(i, j, info, NULL, 0, 0);
715 if (isl_tab_rollback(info[i].tab, snap) < 0)
716 return isl_change_error;
718 return isl_change_none;
722 /* Both basic maps have at least one inequality with and adjacent
723 * (but opposite) inequality in the other basic map.
724 * Check that there are no cut constraints and that there is only
725 * a single pair of adjacent inequalities.
726 * If so, we can replace the pair by a single basic map described
727 * by all but the pair of adjacent inequalities.
728 * Any additional points introduced lie strictly between the two
729 * adjacent hyperplanes and can therefore be integral.
731 * ____ _____
732 * / ||\ / \
733 * / || \ / \
734 * \ || \ => \ \
735 * \ || / \ /
736 * \___||_/ \_____/
738 * The test for a single pair of adjancent inequalities is important
739 * for avoiding the combination of two basic maps like the following
741 * /|
742 * / |
743 * /__|
744 * _____
745 * | |
746 * | |
747 * |___|
749 * If there are some cut constraints on one side, then we may
750 * still be able to fuse the two basic maps, but we need to perform
751 * some additional checks in is_adj_ineq_extension.
753 static enum isl_change check_adj_ineq(int i, int j,
754 struct isl_coalesce_info *info)
756 int count_i, count_j;
757 int cut_i, cut_j;
759 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
760 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
762 if (count_i != 1 && count_j != 1)
763 return isl_change_none;
765 cut_i = any_eq(&info[i], STATUS_CUT) || any_ineq(&info[i], STATUS_CUT);
766 cut_j = any_eq(&info[j], STATUS_CUT) || any_ineq(&info[j], STATUS_CUT);
768 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
769 return fuse(i, j, info, NULL, 0, 0);
771 if (count_i == 1 && !cut_i)
772 return is_adj_ineq_extension(i, j, info);
774 if (count_j == 1 && !cut_j)
775 return is_adj_ineq_extension(j, i, info);
777 return isl_change_none;
780 /* Given an affine transformation matrix "T", does row "row" represent
781 * anything other than a unit vector (possibly shifted by a constant)
782 * that is not involved in any of the other rows?
784 * That is, if a constraint involves the variable corresponding to
785 * the row, then could its preimage by "T" have any coefficients
786 * that are different from those in the original constraint?
788 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
790 int i, j;
791 int len = T->n_col - 1;
793 i = isl_seq_first_non_zero(T->row[row] + 1, len);
794 if (i < 0)
795 return 1;
796 if (!isl_int_is_one(T->row[row][1 + i]) &&
797 !isl_int_is_negone(T->row[row][1 + i]))
798 return 1;
800 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
801 if (j >= 0)
802 return 1;
804 for (j = 1; j < T->n_row; ++j) {
805 if (j == row)
806 continue;
807 if (!isl_int_is_zero(T->row[j][1 + i]))
808 return 1;
811 return 0;
814 /* Does inequality constraint "ineq" of "bmap" involve any of
815 * the variables marked in "affected"?
816 * "total" is the total number of variables, i.e., the number
817 * of entries in "affected".
819 static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
820 int *affected, int total)
822 int i;
824 for (i = 0; i < total; ++i) {
825 if (!affected[i])
826 continue;
827 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
828 return isl_bool_true;
831 return isl_bool_false;
834 /* Given the compressed version of inequality constraint "ineq"
835 * of info->bmap in "v", check if the constraint can be tightened,
836 * where the compression is based on an equality constraint valid
837 * for info->tab.
838 * If so, add the tightened version of the inequality constraint
839 * to info->tab. "v" may be modified by this function.
841 * That is, if the compressed constraint is of the form
843 * m f() + c >= 0
845 * with 0 < c < m, then it is equivalent to
847 * f() >= 0
849 * This means that c can also be subtracted from the original,
850 * uncompressed constraint without affecting the integer points
851 * in info->tab. Add this tightened constraint as an extra row
852 * to info->tab to make this information explicitly available.
854 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
855 int ineq, __isl_take isl_vec *v)
857 isl_ctx *ctx;
858 isl_stat r;
860 if (!v)
861 return NULL;
863 ctx = isl_vec_get_ctx(v);
864 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
865 if (isl_int_is_zero(ctx->normalize_gcd) ||
866 isl_int_is_one(ctx->normalize_gcd)) {
867 return v;
870 v = isl_vec_cow(v);
871 if (!v)
872 return NULL;
874 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
875 if (isl_int_is_zero(v->el[0]))
876 return v;
878 if (isl_tab_extend_cons(info->tab, 1) < 0)
879 return isl_vec_free(v);
881 isl_int_sub(info->bmap->ineq[ineq][0],
882 info->bmap->ineq[ineq][0], v->el[0]);
883 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
884 isl_int_add(info->bmap->ineq[ineq][0],
885 info->bmap->ineq[ineq][0], v->el[0]);
887 if (r < 0)
888 return isl_vec_free(v);
890 return v;
893 /* Tighten the (non-redundant) constraints on the facet represented
894 * by info->tab.
895 * In particular, on input, info->tab represents the result
896 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
897 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
898 * replacing the one at index "l" by the corresponding equality,
899 * i.e., f_k + 1 = 0, with k = relaxed[l].
901 * Compute a variable compression from the equality constraint f_k + 1 = 0
902 * and use it to tighten the other constraints of info->bmap
903 * (that is, all constraints that have not been relaxed),
904 * updating info->tab (and leaving info->bmap untouched).
905 * The compression handles essentially two cases, one where a variable
906 * is assigned a fixed value and can therefore be eliminated, and one
907 * where one variable is a shifted multiple of some other variable and
908 * can therefore be replaced by that multiple.
909 * Gaussian elimination would also work for the first case, but for
910 * the second case, the effectiveness would depend on the order
911 * of the variables.
912 * After compression, some of the constraints may have coefficients
913 * with a common divisor. If this divisor does not divide the constant
914 * term, then the constraint can be tightened.
915 * The tightening is performed on the tableau info->tab by introducing
916 * extra (temporary) constraints.
918 * Only constraints that are possibly affected by the compression are
919 * considered. In particular, if the constraint only involves variables
920 * that are directly mapped to a distinct set of other variables, then
921 * no common divisor can be introduced and no tightening can occur.
923 * It is important to only consider the non-redundant constraints
924 * since the facet constraint has been relaxed prior to the call
925 * to this function, meaning that the constraints that were redundant
926 * prior to the relaxation may no longer be redundant.
927 * These constraints will be ignored in the fused result, so
928 * the fusion detection should not exploit them.
930 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
931 int n, int *relaxed, int l)
933 unsigned total;
934 isl_ctx *ctx;
935 isl_vec *v = NULL;
936 isl_mat *T;
937 int i;
938 int k;
939 int *affected;
941 k = relaxed[l];
942 ctx = isl_basic_map_get_ctx(info->bmap);
943 total = isl_basic_map_total_dim(info->bmap);
944 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
945 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
946 T = isl_mat_variable_compression(T, NULL);
947 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
948 if (!T)
949 return isl_stat_error;
950 if (T->n_col == 0) {
951 isl_mat_free(T);
952 return isl_stat_ok;
955 affected = isl_alloc_array(ctx, int, total);
956 if (!affected)
957 goto error;
959 for (i = 0; i < total; ++i)
960 affected[i] = not_unique_unit_row(T, 1 + i);
962 for (i = 0; i < info->bmap->n_ineq; ++i) {
963 isl_bool handle;
964 if (any(relaxed, n, i))
965 continue;
966 if (info->ineq[i] == STATUS_REDUNDANT)
967 continue;
968 handle = is_affected(info->bmap, i, affected, total);
969 if (handle < 0)
970 goto error;
971 if (!handle)
972 continue;
973 v = isl_vec_alloc(ctx, 1 + total);
974 if (!v)
975 goto error;
976 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
977 v = isl_vec_mat_product(v, isl_mat_copy(T));
978 v = try_tightening(info, i, v);
979 isl_vec_free(v);
980 if (!v)
981 goto error;
984 isl_mat_free(T);
985 free(affected);
986 return isl_stat_ok;
987 error:
988 isl_mat_free(T);
989 free(affected);
990 return isl_stat_error;
993 /* Replace the basic maps "i" and "j" by an extension of "i"
994 * along the "n" inequality constraints in "relax" by one.
995 * The tableau info[i].tab has already been extended.
996 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
997 * by one.
998 * Each integer division that does not have exactly the same
999 * definition in "i" and "j" is marked unknown and the basic map
1000 * is scheduled to be simplified in an attempt to recover
1001 * the integer division definition.
1002 * Place the extension in the position that is the smallest of i and j.
1004 static enum isl_change extend(int i, int j, int n, int *relax,
1005 struct isl_coalesce_info *info)
1007 int l;
1008 unsigned total;
1010 info[i].bmap = isl_basic_map_cow(info[i].bmap);
1011 if (!info[i].bmap)
1012 return isl_change_error;
1013 total = isl_basic_map_total_dim(info[i].bmap);
1014 for (l = 0; l < info[i].bmap->n_div; ++l)
1015 if (!isl_seq_eq(info[i].bmap->div[l],
1016 info[j].bmap->div[l], 1 + 1 + total)) {
1017 isl_int_set_si(info[i].bmap->div[l][0], 0);
1018 info[i].simplify = 1;
1020 for (l = 0; l < n; ++l)
1021 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
1022 info[i].bmap->ineq[relax[l]][0], 1);
1023 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1024 drop(&info[j]);
1025 if (j < i)
1026 exchange(&info[i], &info[j]);
1027 return isl_change_fuse;
1030 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1031 * that are such that they include basic map "j" if they are relaxed
1032 * by one. All the other inequalities are valid for "j".
1033 * Check if basic map "j" forms an extension of basic map "i".
1035 * In particular, relax the constraints in "relax", compute the corresponding
1036 * facets one by one and check whether each of these is included
1037 * in the other basic map.
1038 * Before testing for inclusion, the constraints on each facet
1039 * are tightened to increase the chance of an inclusion being detected.
1040 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1041 * in is_adj_ineq_extension, may further increase those chances, but this
1042 * is not currently done.)
1043 * If each facet is included, we know that relaxing the constraints extends
1044 * the basic map with exactly the other basic map (we already know that this
1045 * other basic map is included in the extension, because all other
1046 * inequality constraints are valid of "j") and we can replace the
1047 * two basic maps by this extension.
1049 * If any of the relaxed constraints turn out to be redundant, then bail out.
1050 * isl_tab_select_facet refuses to handle such constraints. It may be
1051 * possible to handle them anyway by making a distinction between
1052 * redundant constraints with a corresponding facet that still intersects
1053 * the set (allowing isl_tab_select_facet to handle them) and
1054 * those where the facet does not intersect the set (which can be ignored
1055 * because the empty facet is trivially included in the other disjunct).
1056 * However, relaxed constraints that turn out to be redundant should
1057 * be fairly rare and no such instance has been reported where
1058 * coalescing would be successful.
1059 * ____ _____
1060 * / || / |
1061 * / || / |
1062 * \ || => \ |
1063 * \ || \ |
1064 * \___|| \____|
1067 * \ |\
1068 * |\\ | \
1069 * | \\ | \
1070 * | | => | /
1071 * | / | /
1072 * |/ |/
1074 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1075 struct isl_coalesce_info *info)
1077 int l;
1078 isl_bool super;
1079 struct isl_tab_undo *snap, *snap2;
1080 unsigned n_eq = info[i].bmap->n_eq;
1082 for (l = 0; l < n; ++l)
1083 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1084 return isl_change_none;
1086 snap = isl_tab_snap(info[i].tab);
1087 for (l = 0; l < n; ++l)
1088 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1089 return isl_change_error;
1090 for (l = 0; l < n; ++l) {
1091 if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
1092 continue;
1093 if (isl_tab_rollback(info[i].tab, snap) < 0)
1094 return isl_change_error;
1095 return isl_change_none;
1097 snap2 = isl_tab_snap(info[i].tab);
1098 for (l = 0; l < n; ++l) {
1099 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1100 return isl_change_error;
1101 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1102 return isl_change_error;
1103 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1104 return isl_change_error;
1105 super = contains(&info[j], info[i].tab);
1106 if (super < 0)
1107 return isl_change_error;
1108 if (super)
1109 continue;
1110 if (isl_tab_rollback(info[i].tab, snap) < 0)
1111 return isl_change_error;
1112 return isl_change_none;
1115 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1116 return isl_change_error;
1117 return extend(i, j, n, relax, info);
1120 /* Data structure that keeps track of the wrapping constraints
1121 * and of information to bound the coefficients of those constraints.
1123 * bound is set if we want to apply a bound on the coefficients
1124 * mat contains the wrapping constraints
1125 * max is the bound on the coefficients (if bound is set)
1127 struct isl_wraps {
1128 int bound;
1129 isl_mat *mat;
1130 isl_int max;
1133 /* Update wraps->max to be greater than or equal to the coefficients
1134 * in the equalities and inequalities of info->bmap that can be removed
1135 * if we end up applying wrapping.
1137 static isl_stat wraps_update_max(struct isl_wraps *wraps,
1138 struct isl_coalesce_info *info)
1140 int k;
1141 isl_int max_k;
1142 unsigned total = isl_basic_map_total_dim(info->bmap);
1144 isl_int_init(max_k);
1146 for (k = 0; k < info->bmap->n_eq; ++k) {
1147 if (info->eq[2 * k] == STATUS_VALID &&
1148 info->eq[2 * k + 1] == STATUS_VALID)
1149 continue;
1150 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1151 if (isl_int_abs_gt(max_k, wraps->max))
1152 isl_int_set(wraps->max, max_k);
1155 for (k = 0; k < info->bmap->n_ineq; ++k) {
1156 if (info->ineq[k] == STATUS_VALID ||
1157 info->ineq[k] == STATUS_REDUNDANT)
1158 continue;
1159 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1160 if (isl_int_abs_gt(max_k, wraps->max))
1161 isl_int_set(wraps->max, max_k);
1164 isl_int_clear(max_k);
1166 return isl_stat_ok;
1169 /* Initialize the isl_wraps data structure.
1170 * If we want to bound the coefficients of the wrapping constraints,
1171 * we set wraps->max to the largest coefficient
1172 * in the equalities and inequalities that can be removed if we end up
1173 * applying wrapping.
1175 static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1176 struct isl_coalesce_info *info, int i, int j)
1178 isl_ctx *ctx;
1180 wraps->bound = 0;
1181 wraps->mat = mat;
1182 if (!mat)
1183 return isl_stat_error;
1184 ctx = isl_mat_get_ctx(mat);
1185 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1186 if (!wraps->bound)
1187 return isl_stat_ok;
1188 isl_int_init(wraps->max);
1189 isl_int_set_si(wraps->max, 0);
1190 if (wraps_update_max(wraps, &info[i]) < 0)
1191 return isl_stat_error;
1192 if (wraps_update_max(wraps, &info[j]) < 0)
1193 return isl_stat_error;
1195 return isl_stat_ok;
1198 /* Free the contents of the isl_wraps data structure.
1200 static void wraps_free(struct isl_wraps *wraps)
1202 isl_mat_free(wraps->mat);
1203 if (wraps->bound)
1204 isl_int_clear(wraps->max);
1207 /* Is the wrapping constraint in row "row" allowed?
1209 * If wraps->bound is set, we check that none of the coefficients
1210 * is greater than wraps->max.
1212 static int allow_wrap(struct isl_wraps *wraps, int row)
1214 int i;
1216 if (!wraps->bound)
1217 return 1;
1219 for (i = 1; i < wraps->mat->n_col; ++i)
1220 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1221 return 0;
1223 return 1;
1226 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1227 * to include "set" and add the result in position "w" of "wraps".
1228 * "len" is the total number of coefficients in "bound" and "ineq".
1229 * Return 1 on success, 0 on failure and -1 on error.
1230 * Wrapping can fail if the result of wrapping is equal to "bound"
1231 * or if we want to bound the sizes of the coefficients and
1232 * the wrapped constraint does not satisfy this bound.
1234 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1235 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1237 isl_seq_cpy(wraps->mat->row[w], bound, len);
1238 if (negate) {
1239 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1240 ineq = wraps->mat->row[w + 1];
1242 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1243 return -1;
1244 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1245 return 0;
1246 if (!allow_wrap(wraps, w))
1247 return 0;
1248 return 1;
1251 /* For each constraint in info->bmap that is not redundant (as determined
1252 * by info->tab) and that is not a valid constraint for the other basic map,
1253 * wrap the constraint around "bound" such that it includes the whole
1254 * set "set" and append the resulting constraint to "wraps".
1255 * Note that the constraints that are valid for the other basic map
1256 * will be added to the combined basic map by default, so there is
1257 * no need to wrap them.
1258 * The caller wrap_in_facets even relies on this function not wrapping
1259 * any constraints that are already valid.
1260 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1261 * wraps->n_row is the number of actual wrapped constraints that have
1262 * been added.
1263 * If any of the wrapping problems results in a constraint that is
1264 * identical to "bound", then this means that "set" is unbounded in such
1265 * way that no wrapping is possible. If this happens then wraps->n_row
1266 * is reset to zero.
1267 * Similarly, if we want to bound the coefficients of the wrapping
1268 * constraints and a newly added wrapping constraint does not
1269 * satisfy the bound, then wraps->n_row is also reset to zero.
1271 static isl_stat add_wraps(struct isl_wraps *wraps,
1272 struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
1274 int l, m;
1275 int w;
1276 int added;
1277 isl_basic_map *bmap = info->bmap;
1278 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1280 w = wraps->mat->n_row;
1282 for (l = 0; l < bmap->n_ineq; ++l) {
1283 if (info->ineq[l] == STATUS_VALID ||
1284 info->ineq[l] == STATUS_REDUNDANT)
1285 continue;
1286 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1287 continue;
1288 if (isl_seq_eq(bound, bmap->ineq[l], len))
1289 continue;
1290 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1291 continue;
1293 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1294 if (added < 0)
1295 return isl_stat_error;
1296 if (!added)
1297 goto unbounded;
1298 ++w;
1300 for (l = 0; l < bmap->n_eq; ++l) {
1301 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1302 continue;
1303 if (isl_seq_eq(bound, bmap->eq[l], len))
1304 continue;
1306 for (m = 0; m < 2; ++m) {
1307 if (info->eq[2 * l + m] == STATUS_VALID)
1308 continue;
1309 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1310 set, !m);
1311 if (added < 0)
1312 return isl_stat_error;
1313 if (!added)
1314 goto unbounded;
1315 ++w;
1319 wraps->mat->n_row = w;
1320 return isl_stat_ok;
1321 unbounded:
1322 wraps->mat->n_row = 0;
1323 return isl_stat_ok;
1326 /* Check if the constraints in "wraps" from "first" until the last
1327 * are all valid for the basic set represented by "tab".
1328 * If not, wraps->n_row is set to zero.
1330 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1331 struct isl_tab *tab)
1333 int i;
1335 for (i = first; i < wraps->n_row; ++i) {
1336 enum isl_ineq_type type;
1337 type = isl_tab_ineq_type(tab, wraps->row[i]);
1338 if (type == isl_ineq_error)
1339 return -1;
1340 if (type == isl_ineq_redundant)
1341 continue;
1342 wraps->n_row = 0;
1343 return 0;
1346 return 0;
1349 /* Return a set that corresponds to the non-redundant constraints
1350 * (as recorded in tab) of bmap.
1352 * It's important to remove the redundant constraints as some
1353 * of the other constraints may have been modified after the
1354 * constraints were marked redundant.
1355 * In particular, a constraint may have been relaxed.
1356 * Redundant constraints are ignored when a constraint is relaxed
1357 * and should therefore continue to be ignored ever after.
1358 * Otherwise, the relaxation might be thwarted by some of
1359 * these constraints.
1361 * Update the underlying set to ensure that the dimension doesn't change.
1362 * Otherwise the integer divisions could get dropped if the tab
1363 * turns out to be empty.
1365 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1366 struct isl_tab *tab)
1368 isl_basic_set *bset;
1370 bmap = isl_basic_map_copy(bmap);
1371 bset = isl_basic_map_underlying_set(bmap);
1372 bset = isl_basic_set_cow(bset);
1373 bset = isl_basic_set_update_from_tab(bset, tab);
1374 return isl_set_from_basic_set(bset);
1377 /* Wrap the constraints of info->bmap that bound the facet defined
1378 * by inequality "k" around (the opposite of) this inequality to
1379 * include "set". "bound" may be used to store the negated inequality.
1380 * Since the wrapped constraints are not guaranteed to contain the whole
1381 * of info->bmap, we check them in check_wraps.
1382 * If any of the wrapped constraints turn out to be invalid, then
1383 * check_wraps will reset wrap->n_row to zero.
1385 static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
1386 struct isl_coalesce_info *info, int k, isl_int *bound,
1387 __isl_keep isl_set *set)
1389 struct isl_tab_undo *snap;
1390 int n;
1391 unsigned total = isl_basic_map_total_dim(info->bmap);
1393 snap = isl_tab_snap(info->tab);
1395 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1396 return isl_stat_error;
1397 if (isl_tab_detect_redundant(info->tab) < 0)
1398 return isl_stat_error;
1400 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1402 n = wraps->mat->n_row;
1403 if (add_wraps(wraps, info, bound, set) < 0)
1404 return isl_stat_error;
1406 if (isl_tab_rollback(info->tab, snap) < 0)
1407 return isl_stat_error;
1408 if (check_wraps(wraps->mat, n, info->tab) < 0)
1409 return isl_stat_error;
1411 return isl_stat_ok;
1414 /* Given a basic set i with a constraint k that is adjacent to
1415 * basic set j, check if we can wrap
1416 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1417 * (always) around their ridges to include the other set.
1418 * If so, replace the pair of basic sets by their union.
1420 * All constraints of i (except k) are assumed to be valid or
1421 * cut constraints for j.
1422 * Wrapping the cut constraints to include basic map j may result
1423 * in constraints that are no longer valid of basic map i
1424 * we have to check that the resulting wrapping constraints are valid for i.
1425 * If "wrap_facet" is not set, then all constraints of i (except k)
1426 * are assumed to be valid for j.
1427 * ____ _____
1428 * / | / \
1429 * / || / |
1430 * \ || => \ |
1431 * \ || \ |
1432 * \___|| \____|
1435 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1436 struct isl_coalesce_info *info, int wrap_facet)
1438 enum isl_change change = isl_change_none;
1439 struct isl_wraps wraps;
1440 isl_ctx *ctx;
1441 isl_mat *mat;
1442 struct isl_set *set_i = NULL;
1443 struct isl_set *set_j = NULL;
1444 struct isl_vec *bound = NULL;
1445 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1447 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1448 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1449 ctx = isl_basic_map_get_ctx(info[i].bmap);
1450 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1451 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1452 1 + total);
1453 if (wraps_init(&wraps, mat, info, i, j) < 0)
1454 goto error;
1455 bound = isl_vec_alloc(ctx, 1 + total);
1456 if (!set_i || !set_j || !bound)
1457 goto error;
1459 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1460 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1462 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1463 wraps.mat->n_row = 1;
1465 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1466 goto error;
1467 if (!wraps.mat->n_row)
1468 goto unbounded;
1470 if (wrap_facet) {
1471 if (add_wraps_around_facet(&wraps, &info[i], k,
1472 bound->el, set_j) < 0)
1473 goto error;
1474 if (!wraps.mat->n_row)
1475 goto unbounded;
1478 change = fuse(i, j, info, wraps.mat, 0, 0);
1480 unbounded:
1481 wraps_free(&wraps);
1483 isl_set_free(set_i);
1484 isl_set_free(set_j);
1486 isl_vec_free(bound);
1488 return change;
1489 error:
1490 wraps_free(&wraps);
1491 isl_vec_free(bound);
1492 isl_set_free(set_i);
1493 isl_set_free(set_j);
1494 return isl_change_error;
1497 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1498 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1499 * add wrapping constraints to wrap.mat for all constraints
1500 * of basic map j that bound the part of basic map j that sticks out
1501 * of the cut constraint.
1502 * "set_i" is the underlying set of basic map i.
1503 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1505 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1506 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1507 * (with respect to the integer points), so we add t(x) >= 0 instead.
1508 * Otherwise, we wrap the constraints of basic map j that are not
1509 * redundant in this intersection and that are not already valid
1510 * for basic map i over basic map i.
1511 * Note that it is sufficient to wrap the constraints to include
1512 * basic map i, because we will only wrap the constraints that do
1513 * not include basic map i already. The wrapped constraint will
1514 * therefore be more relaxed compared to the original constraint.
1515 * Since the original constraint is valid for basic map j, so is
1516 * the wrapped constraint.
1518 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1519 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1520 struct isl_tab_undo *snap)
1522 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1523 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1524 return isl_stat_error;
1525 if (isl_tab_detect_redundant(info_j->tab) < 0)
1526 return isl_stat_error;
1528 if (info_j->tab->empty)
1529 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1530 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1531 return isl_stat_error;
1533 if (isl_tab_rollback(info_j->tab, snap) < 0)
1534 return isl_stat_error;
1536 return isl_stat_ok;
1539 /* Given a pair of basic maps i and j such that j sticks out
1540 * of i at n cut constraints, each time by at most one,
1541 * try to compute wrapping constraints and replace the two
1542 * basic maps by a single basic map.
1543 * The other constraints of i are assumed to be valid for j.
1544 * "set_i" is the underlying set of basic map i.
1545 * "wraps" has been initialized to be of the right size.
1547 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1548 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1549 * of basic map j that bound the part of basic map j that sticks out
1550 * of the cut constraint.
1552 * If any wrapping fails, i.e., if we cannot wrap to touch
1553 * the union, then we give up.
1554 * Otherwise, the pair of basic maps is replaced by their union.
1556 static enum isl_change try_wrap_in_facets(int i, int j,
1557 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1558 __isl_keep isl_set *set_i)
1560 int k, l, w;
1561 unsigned total;
1562 struct isl_tab_undo *snap;
1564 total = isl_basic_map_total_dim(info[i].bmap);
1566 snap = isl_tab_snap(info[j].tab);
1568 wraps->mat->n_row = 0;
1570 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1571 for (l = 0; l < 2; ++l) {
1572 if (info[i].eq[2 * k + l] != STATUS_CUT)
1573 continue;
1574 w = wraps->mat->n_row++;
1575 if (l == 0)
1576 isl_seq_neg(wraps->mat->row[w],
1577 info[i].bmap->eq[k], 1 + total);
1578 else
1579 isl_seq_cpy(wraps->mat->row[w],
1580 info[i].bmap->eq[k], 1 + total);
1581 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1582 return isl_change_error;
1584 if (!wraps->mat->n_row)
1585 return isl_change_none;
1589 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1590 if (info[i].ineq[k] != STATUS_CUT)
1591 continue;
1592 w = wraps->mat->n_row++;
1593 isl_seq_cpy(wraps->mat->row[w],
1594 info[i].bmap->ineq[k], 1 + total);
1595 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1596 return isl_change_error;
1598 if (!wraps->mat->n_row)
1599 return isl_change_none;
1602 return fuse(i, j, info, wraps->mat, 0, 1);
1605 /* Given a pair of basic maps i and j such that j sticks out
1606 * of i at n cut constraints, each time by at most one,
1607 * try to compute wrapping constraints and replace the two
1608 * basic maps by a single basic map.
1609 * The other constraints of i are assumed to be valid for j.
1611 * The core computation is performed by try_wrap_in_facets.
1612 * This function simply extracts an underlying set representation
1613 * of basic map i and initializes the data structure for keeping
1614 * track of wrapping constraints.
1616 static enum isl_change wrap_in_facets(int i, int j, int n,
1617 struct isl_coalesce_info *info)
1619 enum isl_change change = isl_change_none;
1620 struct isl_wraps wraps;
1621 isl_ctx *ctx;
1622 isl_mat *mat;
1623 isl_set *set_i = NULL;
1624 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1625 int max_wrap;
1627 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1628 return isl_change_error;
1630 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1631 max_wrap *= n;
1633 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1634 ctx = isl_basic_map_get_ctx(info[i].bmap);
1635 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1636 if (wraps_init(&wraps, mat, info, i, j) < 0)
1637 goto error;
1638 if (!set_i)
1639 goto error;
1641 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1643 wraps_free(&wraps);
1644 isl_set_free(set_i);
1646 return change;
1647 error:
1648 wraps_free(&wraps);
1649 isl_set_free(set_i);
1650 return isl_change_error;
1653 /* Return the effect of inequality "ineq" on the tableau "tab",
1654 * after relaxing the constant term of "ineq" by one.
1656 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1658 enum isl_ineq_type type;
1660 isl_int_add_ui(ineq[0], ineq[0], 1);
1661 type = isl_tab_ineq_type(tab, ineq);
1662 isl_int_sub_ui(ineq[0], ineq[0], 1);
1664 return type;
1667 /* Given two basic sets i and j,
1668 * check if relaxing all the cut constraints of i by one turns
1669 * them into valid constraint for j and check if we can wrap in
1670 * the bits that are sticking out.
1671 * If so, replace the pair by their union.
1673 * We first check if all relaxed cut inequalities of i are valid for j
1674 * and then try to wrap in the intersections of the relaxed cut inequalities
1675 * with j.
1677 * During this wrapping, we consider the points of j that lie at a distance
1678 * of exactly 1 from i. In particular, we ignore the points that lie in
1679 * between this lower-dimensional space and the basic map i.
1680 * We can therefore only apply this to integer maps.
1681 * ____ _____
1682 * / ___|_ / \
1683 * / | | / |
1684 * \ | | => \ |
1685 * \|____| \ |
1686 * \___| \____/
1688 * _____ ______
1689 * | ____|_ | \
1690 * | | | | |
1691 * | | | => | |
1692 * |_| | | |
1693 * |_____| \______|
1695 * _______
1696 * | |
1697 * | |\ |
1698 * | | \ |
1699 * | | \ |
1700 * | | \|
1701 * | | \
1702 * | |_____\
1703 * | |
1704 * |_______|
1706 * Wrapping can fail if the result of wrapping one of the facets
1707 * around its edges does not produce any new facet constraint.
1708 * In particular, this happens when we try to wrap in unbounded sets.
1710 * _______________________________________________________________________
1712 * | ___
1713 * | | |
1714 * |_| |_________________________________________________________________
1715 * |___|
1717 * The following is not an acceptable result of coalescing the above two
1718 * sets as it includes extra integer points.
1719 * _______________________________________________________________________
1721 * |
1722 * |
1724 * \______________________________________________________________________
1726 static enum isl_change can_wrap_in_set(int i, int j,
1727 struct isl_coalesce_info *info)
1729 int k, l;
1730 int n;
1731 unsigned total;
1733 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1734 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1735 return isl_change_none;
1737 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1738 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1739 if (n == 0)
1740 return isl_change_none;
1742 total = isl_basic_map_total_dim(info[i].bmap);
1743 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1744 for (l = 0; l < 2; ++l) {
1745 enum isl_ineq_type type;
1747 if (info[i].eq[2 * k + l] != STATUS_CUT)
1748 continue;
1750 if (l == 0)
1751 isl_seq_neg(info[i].bmap->eq[k],
1752 info[i].bmap->eq[k], 1 + total);
1753 type = type_of_relaxed(info[j].tab,
1754 info[i].bmap->eq[k]);
1755 if (l == 0)
1756 isl_seq_neg(info[i].bmap->eq[k],
1757 info[i].bmap->eq[k], 1 + total);
1758 if (type == isl_ineq_error)
1759 return isl_change_error;
1760 if (type != isl_ineq_redundant)
1761 return isl_change_none;
1765 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1766 enum isl_ineq_type type;
1768 if (info[i].ineq[k] != STATUS_CUT)
1769 continue;
1771 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1772 if (type == isl_ineq_error)
1773 return isl_change_error;
1774 if (type != isl_ineq_redundant)
1775 return isl_change_none;
1778 return wrap_in_facets(i, j, n, info);
1781 /* Check if either i or j has only cut constraints that can
1782 * be used to wrap in (a facet of) the other basic set.
1783 * if so, replace the pair by their union.
1785 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1787 enum isl_change change = isl_change_none;
1789 change = can_wrap_in_set(i, j, info);
1790 if (change != isl_change_none)
1791 return change;
1793 change = can_wrap_in_set(j, i, info);
1794 return change;
1797 /* Check if all inequality constraints of "i" that cut "j" cease
1798 * to be cut constraints if they are relaxed by one.
1799 * If so, collect the cut constraints in "list".
1800 * The caller is responsible for allocating "list".
1802 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1803 int *list)
1805 int l, n;
1807 n = 0;
1808 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1809 enum isl_ineq_type type;
1811 if (info[i].ineq[l] != STATUS_CUT)
1812 continue;
1813 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1814 if (type == isl_ineq_error)
1815 return isl_bool_error;
1816 if (type != isl_ineq_redundant)
1817 return isl_bool_false;
1818 list[n++] = l;
1821 return isl_bool_true;
1824 /* Given two basic maps such that "j" has at least one equality constraint
1825 * that is adjacent to an inequality constraint of "i" and such that "i" has
1826 * exactly one inequality constraint that is adjacent to an equality
1827 * constraint of "j", check whether "i" can be extended to include "j" or
1828 * whether "j" can be wrapped into "i".
1829 * All remaining constraints of "i" and "j" are assumed to be valid
1830 * or cut constraints of the other basic map.
1831 * However, none of the equality constraints of "i" are cut constraints.
1833 * If "i" has any "cut" inequality constraints, then check if relaxing
1834 * each of them by one is sufficient for them to become valid.
1835 * If so, check if the inequality constraint adjacent to an equality
1836 * constraint of "j" along with all these cut constraints
1837 * can be relaxed by one to contain exactly "j".
1838 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1840 static enum isl_change check_single_adj_eq(int i, int j,
1841 struct isl_coalesce_info *info)
1843 enum isl_change change = isl_change_none;
1844 int k;
1845 int n_cut;
1846 int *relax;
1847 isl_ctx *ctx;
1848 isl_bool try_relax;
1850 n_cut = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1852 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ);
1854 if (n_cut > 0) {
1855 ctx = isl_basic_map_get_ctx(info[i].bmap);
1856 relax = isl_calloc_array(ctx, int, 1 + n_cut);
1857 if (!relax)
1858 return isl_change_error;
1859 relax[0] = k;
1860 try_relax = all_cut_by_one(i, j, info, relax + 1);
1861 if (try_relax < 0)
1862 change = isl_change_error;
1863 } else {
1864 try_relax = isl_bool_true;
1865 relax = &k;
1867 if (try_relax && change == isl_change_none)
1868 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
1869 if (n_cut > 0)
1870 free(relax);
1871 if (change != isl_change_none)
1872 return change;
1874 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
1876 return change;
1879 /* At least one of the basic maps has an equality that is adjacent
1880 * to an inequality. Make sure that only one of the basic maps has
1881 * such an equality and that the other basic map has exactly one
1882 * inequality adjacent to an equality.
1883 * If the other basic map does not have such an inequality, then
1884 * check if all its constraints are either valid or cut constraints
1885 * and, if so, try wrapping in the first map into the second.
1886 * Otherwise, try to extend one basic map with the other or
1887 * wrap one basic map in the other.
1889 static enum isl_change check_adj_eq(int i, int j,
1890 struct isl_coalesce_info *info)
1892 if (any_eq(&info[i], STATUS_ADJ_INEQ) &&
1893 any_eq(&info[j], STATUS_ADJ_INEQ))
1894 /* ADJ EQ TOO MANY */
1895 return isl_change_none;
1897 if (any_eq(&info[i], STATUS_ADJ_INEQ))
1898 return check_adj_eq(j, i, info);
1900 /* j has an equality adjacent to an inequality in i */
1902 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1903 if (all_valid_or_cut(&info[i]))
1904 return can_wrap_in_set(i, j, info);
1905 return isl_change_none;
1907 if (any_eq(&info[i], STATUS_CUT))
1908 return isl_change_none;
1909 if (any_ineq(&info[j], STATUS_ADJ_EQ) ||
1910 any_ineq(&info[i], STATUS_ADJ_INEQ) ||
1911 any_ineq(&info[j], STATUS_ADJ_INEQ))
1912 /* ADJ EQ TOO MANY */
1913 return isl_change_none;
1915 return check_single_adj_eq(i, j, info);
1918 /* The two basic maps lie on adjacent hyperplanes. In particular,
1919 * basic map "i" has an equality that lies parallel to basic map "j".
1920 * Check if we can wrap the facets around the parallel hyperplanes
1921 * to include the other set.
1923 * We perform basically the same operations as can_wrap_in_facet,
1924 * except that we don't need to select a facet of one of the sets.
1926 * \\ \\
1927 * \\ => \\
1928 * \ \|
1930 * If there is more than one equality of "i" adjacent to an equality of "j",
1931 * then the result will satisfy one or more equalities that are a linear
1932 * combination of these equalities. These will be encoded as pairs
1933 * of inequalities in the wrapping constraints and need to be made
1934 * explicit.
1936 static enum isl_change check_eq_adj_eq(int i, int j,
1937 struct isl_coalesce_info *info)
1939 int k;
1940 enum isl_change change = isl_change_none;
1941 int detect_equalities = 0;
1942 struct isl_wraps wraps;
1943 isl_ctx *ctx;
1944 isl_mat *mat;
1945 struct isl_set *set_i = NULL;
1946 struct isl_set *set_j = NULL;
1947 struct isl_vec *bound = NULL;
1948 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1950 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1951 detect_equalities = 1;
1953 k = find(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ);
1955 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1956 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1957 ctx = isl_basic_map_get_ctx(info[i].bmap);
1958 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1959 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1960 1 + total);
1961 if (wraps_init(&wraps, mat, info, i, j) < 0)
1962 goto error;
1963 bound = isl_vec_alloc(ctx, 1 + total);
1964 if (!set_i || !set_j || !bound)
1965 goto error;
1967 if (k % 2 == 0)
1968 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1969 else
1970 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1971 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1973 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1974 wraps.mat->n_row = 1;
1976 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1977 goto error;
1978 if (!wraps.mat->n_row)
1979 goto unbounded;
1981 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1982 isl_seq_neg(bound->el, bound->el, 1 + total);
1984 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1985 wraps.mat->n_row++;
1987 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1988 goto error;
1989 if (!wraps.mat->n_row)
1990 goto unbounded;
1992 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1994 if (0) {
1995 error: change = isl_change_error;
1997 unbounded:
1999 wraps_free(&wraps);
2000 isl_set_free(set_i);
2001 isl_set_free(set_j);
2002 isl_vec_free(bound);
2004 return change;
2007 /* Initialize the "eq" and "ineq" fields of "info".
2009 static void init_status(struct isl_coalesce_info *info)
2011 info->eq = info->ineq = NULL;
2014 /* Set info->eq to the positions of the equalities of info->bmap
2015 * with respect to the basic map represented by "tab".
2016 * If info->eq has already been computed, then do not compute it again.
2018 static void set_eq_status_in(struct isl_coalesce_info *info,
2019 struct isl_tab *tab)
2021 if (info->eq)
2022 return;
2023 info->eq = eq_status_in(info->bmap, tab);
2026 /* Set info->ineq to the positions of the inequalities of info->bmap
2027 * with respect to the basic map represented by "tab".
2028 * If info->ineq has already been computed, then do not compute it again.
2030 static void set_ineq_status_in(struct isl_coalesce_info *info,
2031 struct isl_tab *tab)
2033 if (info->ineq)
2034 return;
2035 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
2038 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2039 * This function assumes that init_status has been called on "info" first,
2040 * after which the "eq" and "ineq" fields may or may not have been
2041 * assigned a newly allocated array.
2043 static void clear_status(struct isl_coalesce_info *info)
2045 free(info->eq);
2046 free(info->ineq);
2049 /* Are all inequality constraints of the basic map represented by "info"
2050 * valid for the other basic map, except for a single constraint
2051 * that is adjacent to an inequality constraint of the other basic map?
2053 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2055 int i;
2056 int k = -1;
2058 for (i = 0; i < info->bmap->n_ineq; ++i) {
2059 if (info->ineq[i] == STATUS_REDUNDANT)
2060 continue;
2061 if (info->ineq[i] == STATUS_VALID)
2062 continue;
2063 if (info->ineq[i] != STATUS_ADJ_INEQ)
2064 return 0;
2065 if (k != -1)
2066 return 0;
2067 k = i;
2070 return k != -1;
2073 /* Basic map "i" has one or more equality constraints that separate it
2074 * from basic map "j". Check if it happens to be an extension
2075 * of basic map "j".
2076 * In particular, check that all constraints of "j" are valid for "i",
2077 * except for one inequality constraint that is adjacent
2078 * to an inequality constraints of "i".
2079 * If so, check for "i" being an extension of "j" by calling
2080 * is_adj_ineq_extension.
2082 * Clean up the memory allocated for keeping track of the status
2083 * of the constraints before returning.
2085 static enum isl_change separating_equality(int i, int j,
2086 struct isl_coalesce_info *info)
2088 enum isl_change change = isl_change_none;
2090 if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2091 all_ineq_valid_or_single_adj_ineq(&info[j]))
2092 change = is_adj_ineq_extension(j, i, info);
2094 clear_status(&info[i]);
2095 clear_status(&info[j]);
2096 return change;
2099 /* Check if the union of the given pair of basic maps
2100 * can be represented by a single basic map.
2101 * If so, replace the pair by the single basic map and return
2102 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2103 * Otherwise, return isl_change_none.
2104 * The two basic maps are assumed to live in the same local space.
2105 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2106 * to have been initialized by the caller, either to NULL or
2107 * to valid information.
2109 * We first check the effect of each constraint of one basic map
2110 * on the other basic map.
2111 * The constraint may be
2112 * redundant the constraint is redundant in its own
2113 * basic map and should be ignore and removed
2114 * in the end
2115 * valid all (integer) points of the other basic map
2116 * satisfy the constraint
2117 * separate no (integer) point of the other basic map
2118 * satisfies the constraint
2119 * cut some but not all points of the other basic map
2120 * satisfy the constraint
2121 * adj_eq the given constraint is adjacent (on the outside)
2122 * to an equality of the other basic map
2123 * adj_ineq the given constraint is adjacent (on the outside)
2124 * to an inequality of the other basic map
2126 * We consider seven cases in which we can replace the pair by a single
2127 * basic map. We ignore all "redundant" constraints.
2129 * 1. all constraints of one basic map are valid
2130 * => the other basic map is a subset and can be removed
2132 * 2. all constraints of both basic maps are either "valid" or "cut"
2133 * and the facets corresponding to the "cut" constraints
2134 * of one of the basic maps lies entirely inside the other basic map
2135 * => the pair can be replaced by a basic map consisting
2136 * of the valid constraints in both basic maps
2138 * 3. there is a single pair of adjacent inequalities
2139 * (all other constraints are "valid")
2140 * => the pair can be replaced by a basic map consisting
2141 * of the valid constraints in both basic maps
2143 * 4. one basic map has a single adjacent inequality, while the other
2144 * constraints are "valid". The other basic map has some
2145 * "cut" constraints, but replacing the adjacent inequality by
2146 * its opposite and adding the valid constraints of the other
2147 * basic map results in a subset of the other basic map
2148 * => the pair can be replaced by a basic map consisting
2149 * of the valid constraints in both basic maps
2151 * 5. there is a single adjacent pair of an inequality and an equality,
2152 * the other constraints of the basic map containing the inequality are
2153 * "valid". Moreover, if the inequality the basic map is relaxed
2154 * and then turned into an equality, then resulting facet lies
2155 * entirely inside the other basic map
2156 * => the pair can be replaced by the basic map containing
2157 * the inequality, with the inequality relaxed.
2159 * 6. there is a single adjacent pair of an inequality and an equality,
2160 * the other constraints of the basic map containing the inequality are
2161 * "valid". Moreover, the facets corresponding to both
2162 * the inequality and the equality can be wrapped around their
2163 * ridges to include the other basic map
2164 * => the pair can be replaced by a basic map consisting
2165 * of the valid constraints in both basic maps together
2166 * with all wrapping constraints
2168 * 7. one of the basic maps extends beyond the other by at most one.
2169 * Moreover, the facets corresponding to the cut constraints and
2170 * the pieces of the other basic map at offset one from these cut
2171 * constraints can be wrapped around their ridges to include
2172 * the union of the two basic maps
2173 * => the pair can be replaced by a basic map consisting
2174 * of the valid constraints in both basic maps together
2175 * with all wrapping constraints
2177 * 8. the two basic maps live in adjacent hyperplanes. In principle
2178 * such sets can always be combined through wrapping, but we impose
2179 * that there is only one such pair, to avoid overeager coalescing.
2181 * Throughout the computation, we maintain a collection of tableaus
2182 * corresponding to the basic maps. When the basic maps are dropped
2183 * or combined, the tableaus are modified accordingly.
2185 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2186 struct isl_coalesce_info *info)
2188 enum isl_change change = isl_change_none;
2190 set_ineq_status_in(&info[i], info[j].tab);
2191 if (info[i].bmap->n_ineq && !info[i].ineq)
2192 goto error;
2193 if (any_ineq(&info[i], STATUS_ERROR))
2194 goto error;
2195 if (any_ineq(&info[i], STATUS_SEPARATE))
2196 goto done;
2198 set_ineq_status_in(&info[j], info[i].tab);
2199 if (info[j].bmap->n_ineq && !info[j].ineq)
2200 goto error;
2201 if (any_ineq(&info[j], STATUS_ERROR))
2202 goto error;
2203 if (any_ineq(&info[j], STATUS_SEPARATE))
2204 goto done;
2206 set_eq_status_in(&info[i], info[j].tab);
2207 if (info[i].bmap->n_eq && !info[i].eq)
2208 goto error;
2209 if (any_eq(&info[i], STATUS_ERROR))
2210 goto error;
2212 set_eq_status_in(&info[j], info[i].tab);
2213 if (info[j].bmap->n_eq && !info[j].eq)
2214 goto error;
2215 if (any_eq(&info[j], STATUS_ERROR))
2216 goto error;
2218 if (any_eq(&info[i], STATUS_SEPARATE))
2219 return separating_equality(i, j, info);
2220 if (any_eq(&info[j], STATUS_SEPARATE))
2221 return separating_equality(j, i, info);
2223 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2224 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2225 drop(&info[j]);
2226 change = isl_change_drop_second;
2227 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2228 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2229 drop(&info[i]);
2230 change = isl_change_drop_first;
2231 } else if (any_eq(&info[i], STATUS_ADJ_EQ)) {
2232 change = check_eq_adj_eq(i, j, info);
2233 } else if (any_eq(&info[j], STATUS_ADJ_EQ)) {
2234 change = check_eq_adj_eq(j, i, info);
2235 } else if (any_eq(&info[i], STATUS_ADJ_INEQ) ||
2236 any_eq(&info[j], STATUS_ADJ_INEQ)) {
2237 change = check_adj_eq(i, j, info);
2238 } else if (any_ineq(&info[i], STATUS_ADJ_EQ) ||
2239 any_ineq(&info[j], STATUS_ADJ_EQ)) {
2240 /* Can't happen */
2241 /* BAD ADJ INEQ */
2242 } else if (any_ineq(&info[i], STATUS_ADJ_INEQ) ||
2243 any_ineq(&info[j], STATUS_ADJ_INEQ)) {
2244 change = check_adj_ineq(i, j, info);
2245 } else {
2246 if (!any_eq(&info[i], STATUS_CUT) &&
2247 !any_eq(&info[j], STATUS_CUT))
2248 change = check_facets(i, j, info);
2249 if (change == isl_change_none)
2250 change = check_wrap(i, j, info);
2253 done:
2254 clear_status(&info[i]);
2255 clear_status(&info[j]);
2256 return change;
2257 error:
2258 clear_status(&info[i]);
2259 clear_status(&info[j]);
2260 return isl_change_error;
2263 /* Check if the union of the given pair of basic maps
2264 * can be represented by a single basic map.
2265 * If so, replace the pair by the single basic map and return
2266 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2267 * Otherwise, return isl_change_none.
2268 * The two basic maps are assumed to live in the same local space.
2270 static enum isl_change coalesce_local_pair(int i, int j,
2271 struct isl_coalesce_info *info)
2273 init_status(&info[i]);
2274 init_status(&info[j]);
2275 return coalesce_local_pair_reuse(i, j, info);
2278 /* Shift the integer division at position "div" of the basic map
2279 * represented by "info" by "shift".
2281 * That is, if the integer division has the form
2283 * floor(f(x)/d)
2285 * then replace it by
2287 * floor((f(x) + shift * d)/d) - shift
2289 static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2290 isl_int shift)
2292 unsigned total;
2294 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2295 if (!info->bmap)
2296 return isl_stat_error;
2298 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2299 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2300 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2301 return isl_stat_error;
2303 return isl_stat_ok;
2306 /* If the integer division at position "div" is defined by an equality,
2307 * i.e., a stride constraint, then change the integer division expression
2308 * to have a constant term equal to zero.
2310 * Let the equality constraint be
2312 * c + f + m a = 0
2314 * The integer division expression is then of the form
2316 * a = floor((-f - c')/m)
2318 * The integer division is first shifted by t = floor(c/m),
2319 * turning the equality constraint into
2321 * c - m floor(c/m) + f + m a' = 0
2323 * i.e.,
2325 * (c mod m) + f + m a' = 0
2327 * That is,
2329 * a' = (-f - (c mod m))/m = floor((-f)/m)
2331 * because a' is an integer and 0 <= (c mod m) < m.
2332 * The constant term of a' can therefore be zeroed out.
2334 static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2336 isl_bool defined;
2337 isl_stat r;
2338 isl_constraint *c;
2339 isl_int shift, stride;
2341 defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2342 div, &c);
2343 if (defined < 0)
2344 return isl_stat_error;
2345 if (!defined)
2346 return isl_stat_ok;
2347 if (!c)
2348 return isl_stat_error;
2349 isl_int_init(shift);
2350 isl_int_init(stride);
2351 isl_constraint_get_constant(c, &shift);
2352 isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2353 isl_int_fdiv_q(shift, shift, stride);
2354 r = shift_div(info, div, shift);
2355 isl_int_clear(stride);
2356 isl_int_clear(shift);
2357 isl_constraint_free(c);
2358 if (r < 0)
2359 return isl_stat_error;
2360 info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2361 info->bmap, div, 0);
2362 if (!info->bmap)
2363 return isl_stat_error;
2364 return isl_stat_ok;
2367 /* The basic maps represented by "info1" and "info2" are known
2368 * to have the same number of integer divisions.
2369 * Check if pairs of integer divisions are equal to each other
2370 * despite the fact that they differ by a rational constant.
2372 * In particular, look for any pair of integer divisions that
2373 * only differ in their constant terms.
2374 * If either of these integer divisions is defined
2375 * by stride constraints, then modify it to have a zero constant term.
2376 * If both are defined by stride constraints then in the end they will have
2377 * the same (zero) constant term.
2379 static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2380 struct isl_coalesce_info *info2)
2382 int i, n;
2384 n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2385 for (i = 0; i < n; ++i) {
2386 isl_bool known, harmonize;
2388 known = isl_basic_map_div_is_known(info1->bmap, i);
2389 if (known >= 0 && known)
2390 known = isl_basic_map_div_is_known(info2->bmap, i);
2391 if (known < 0)
2392 return isl_stat_error;
2393 if (!known)
2394 continue;
2395 harmonize = isl_basic_map_equal_div_expr_except_constant(
2396 info1->bmap, i, info2->bmap, i);
2397 if (harmonize < 0)
2398 return isl_stat_error;
2399 if (!harmonize)
2400 continue;
2401 if (normalize_stride_div(info1, i) < 0)
2402 return isl_stat_error;
2403 if (normalize_stride_div(info2, i) < 0)
2404 return isl_stat_error;
2407 return isl_stat_ok;
2410 /* If "shift" is an integer constant, then shift the integer division
2411 * at position "div" of the basic map represented by "info" by "shift".
2412 * If "shift" is not an integer constant, then do nothing.
2413 * If "shift" is equal to zero, then no shift needs to be performed either.
2415 * That is, if the integer division has the form
2417 * floor(f(x)/d)
2419 * then replace it by
2421 * floor((f(x) + shift * d)/d) - shift
2423 static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2424 __isl_keep isl_aff *shift)
2426 isl_bool cst;
2427 isl_stat r;
2428 isl_int d;
2429 isl_val *c;
2431 cst = isl_aff_is_cst(shift);
2432 if (cst < 0 || !cst)
2433 return cst < 0 ? isl_stat_error : isl_stat_ok;
2435 c = isl_aff_get_constant_val(shift);
2436 cst = isl_val_is_int(c);
2437 if (cst >= 0 && cst)
2438 cst = isl_bool_not(isl_val_is_zero(c));
2439 if (cst < 0 || !cst) {
2440 isl_val_free(c);
2441 return cst < 0 ? isl_stat_error : isl_stat_ok;
2444 isl_int_init(d);
2445 r = isl_val_get_num_isl_int(c, &d);
2446 if (r >= 0)
2447 r = shift_div(info, div, d);
2448 isl_int_clear(d);
2450 isl_val_free(c);
2452 return r;
2455 /* Check if some of the divs in the basic map represented by "info1"
2456 * are shifts of the corresponding divs in the basic map represented
2457 * by "info2", taking into account the equality constraints "eq1" of "info1"
2458 * and "eq2" of "info2". If so, align them with those of "info2".
2459 * "info1" and "info2" are assumed to have the same number
2460 * of integer divisions.
2462 * An integer division is considered to be a shift of another integer
2463 * division if, after simplification with respect to the equality
2464 * constraints of the other basic map, one is equal to the other
2465 * plus a constant.
2467 * In particular, for each pair of integer divisions, if both are known,
2468 * have the same denominator and are not already equal to each other,
2469 * simplify each with respect to the equality constraints
2470 * of the other basic map. If the difference is an integer constant,
2471 * then move this difference outside.
2472 * That is, if, after simplification, one integer division is of the form
2474 * floor((f(x) + c_1)/d)
2476 * while the other is of the form
2478 * floor((f(x) + c_2)/d)
2480 * and n = (c_2 - c_1)/d is an integer, then replace the first
2481 * integer division by
2483 * floor((f_1(x) + c_1 + n * d)/d) - n,
2485 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2486 * after simplification with respect to the equality constraints.
2488 static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2489 struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2490 __isl_keep isl_basic_set *eq2)
2492 int i;
2493 int total;
2494 isl_local_space *ls1, *ls2;
2496 total = isl_basic_map_total_dim(info1->bmap);
2497 ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2498 ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2499 for (i = 0; i < info1->bmap->n_div; ++i) {
2500 isl_stat r;
2501 isl_aff *div1, *div2;
2503 if (!isl_local_space_div_is_known(ls1, i) ||
2504 !isl_local_space_div_is_known(ls2, i))
2505 continue;
2506 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2507 continue;
2508 if (isl_seq_eq(info1->bmap->div[i] + 1,
2509 info2->bmap->div[i] + 1, 1 + total))
2510 continue;
2511 div1 = isl_local_space_get_div(ls1, i);
2512 div2 = isl_local_space_get_div(ls2, i);
2513 div1 = isl_aff_substitute_equalities(div1,
2514 isl_basic_set_copy(eq2));
2515 div2 = isl_aff_substitute_equalities(div2,
2516 isl_basic_set_copy(eq1));
2517 div2 = isl_aff_sub(div2, div1);
2518 r = shift_if_cst_int(info1, i, div2);
2519 isl_aff_free(div2);
2520 if (r < 0)
2521 break;
2523 isl_local_space_free(ls1);
2524 isl_local_space_free(ls2);
2526 if (i < info1->bmap->n_div)
2527 return isl_stat_error;
2528 return isl_stat_ok;
2531 /* Check if some of the divs in the basic map represented by "info1"
2532 * are shifts of the corresponding divs in the basic map represented
2533 * by "info2". If so, align them with those of "info2".
2534 * Only do this if "info1" and "info2" have the same number
2535 * of integer divisions.
2537 * An integer division is considered to be a shift of another integer
2538 * division if, after simplification with respect to the equality
2539 * constraints of the other basic map, one is equal to the other
2540 * plus a constant.
2542 * First check if pairs of integer divisions are equal to each other
2543 * despite the fact that they differ by a rational constant.
2544 * If so, try and arrange for them to have the same constant term.
2546 * Then, extract the equality constraints and continue with
2547 * harmonize_divs_with_hulls.
2549 * If the equality constraints of both basic maps are the same,
2550 * then there is no need to perform any shifting since
2551 * the coefficients of the integer divisions should have been
2552 * reduced in the same way.
2554 static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2555 struct isl_coalesce_info *info2)
2557 isl_bool equal;
2558 isl_basic_map *bmap1, *bmap2;
2559 isl_basic_set *eq1, *eq2;
2560 isl_stat r;
2562 if (!info1->bmap || !info2->bmap)
2563 return isl_stat_error;
2565 if (info1->bmap->n_div != info2->bmap->n_div)
2566 return isl_stat_ok;
2567 if (info1->bmap->n_div == 0)
2568 return isl_stat_ok;
2570 if (harmonize_stride_divs(info1, info2) < 0)
2571 return isl_stat_error;
2573 bmap1 = isl_basic_map_copy(info1->bmap);
2574 bmap2 = isl_basic_map_copy(info2->bmap);
2575 eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2576 eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2577 equal = isl_basic_set_plain_is_equal(eq1, eq2);
2578 if (equal < 0)
2579 r = isl_stat_error;
2580 else if (equal)
2581 r = isl_stat_ok;
2582 else
2583 r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2584 isl_basic_set_free(eq1);
2585 isl_basic_set_free(eq2);
2587 return r;
2590 /* Do the two basic maps live in the same local space, i.e.,
2591 * do they have the same (known) divs?
2592 * If either basic map has any unknown divs, then we can only assume
2593 * that they do not live in the same local space.
2595 static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
2596 __isl_keep isl_basic_map *bmap2)
2598 int i;
2599 isl_bool known;
2600 int total;
2602 if (!bmap1 || !bmap2)
2603 return isl_bool_error;
2604 if (bmap1->n_div != bmap2->n_div)
2605 return isl_bool_false;
2607 if (bmap1->n_div == 0)
2608 return isl_bool_true;
2610 known = isl_basic_map_divs_known(bmap1);
2611 if (known < 0 || !known)
2612 return known;
2613 known = isl_basic_map_divs_known(bmap2);
2614 if (known < 0 || !known)
2615 return known;
2617 total = isl_basic_map_total_dim(bmap1);
2618 for (i = 0; i < bmap1->n_div; ++i)
2619 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2620 return 0;
2622 return 1;
2625 /* Assuming that "tab" contains the equality constraints and
2626 * the initial inequality constraints of "bmap", copy the remaining
2627 * inequality constraints of "bmap" to "Tab".
2629 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2631 int i, n_ineq;
2633 if (!bmap)
2634 return isl_stat_error;
2636 n_ineq = tab->n_con - tab->n_eq;
2637 for (i = n_ineq; i < bmap->n_ineq; ++i)
2638 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2639 return isl_stat_error;
2641 return isl_stat_ok;
2644 /* Description of an integer division that is added
2645 * during an expansion.
2646 * "pos" is the position of the corresponding variable.
2647 * "cst" indicates whether this integer division has a fixed value.
2648 * "val" contains the fixed value, if the value is fixed.
2650 struct isl_expanded {
2651 int pos;
2652 isl_bool cst;
2653 isl_int val;
2656 /* For each of the "n" integer division variables "expanded",
2657 * if the variable has a fixed value, then add two inequality
2658 * constraints expressing the fixed value.
2659 * Otherwise, add the corresponding div constraints.
2660 * The caller is responsible for removing the div constraints
2661 * that it added for all these "n" integer divisions.
2663 * The div constraints and the pair of inequality constraints
2664 * forcing the fixed value cannot both be added for a given variable
2665 * as the combination may render some of the original constraints redundant.
2666 * These would then be ignored during the coalescing detection,
2667 * while they could remain in the fused result.
2669 * The two added inequality constraints are
2671 * -a + v >= 0
2672 * a - v >= 0
2674 * with "a" the variable and "v" its fixed value.
2675 * The facet corresponding to one of these two constraints is selected
2676 * in the tableau to ensure that the pair of inequality constraints
2677 * is treated as an equality constraint.
2679 * The information in info->ineq is thrown away because it was
2680 * computed in terms of div constraints, while some of those
2681 * have now been replaced by these pairs of inequality constraints.
2683 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2684 int n, struct isl_expanded *expanded)
2686 unsigned o_div;
2687 int i;
2688 isl_vec *ineq;
2690 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2691 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2692 if (!ineq)
2693 return isl_stat_error;
2694 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2696 for (i = 0; i < n; ++i) {
2697 if (!expanded[i].cst) {
2698 info->bmap = isl_basic_map_extend_constraints(
2699 info->bmap, 0, 2);
2700 if (isl_basic_map_add_div_constraints(info->bmap,
2701 expanded[i].pos - o_div) < 0)
2702 break;
2703 } else {
2704 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2705 isl_int_set(ineq->el[0], expanded[i].val);
2706 info->bmap = isl_basic_map_add_ineq(info->bmap,
2707 ineq->el);
2708 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2709 isl_int_neg(ineq->el[0], expanded[i].val);
2710 info->bmap = isl_basic_map_add_ineq(info->bmap,
2711 ineq->el);
2712 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2714 if (copy_ineq(info->tab, info->bmap) < 0)
2715 break;
2716 if (expanded[i].cst &&
2717 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2718 break;
2721 isl_vec_free(ineq);
2723 clear_status(info);
2724 init_status(info);
2726 return i < n ? isl_stat_error : isl_stat_ok;
2729 /* Insert the "n" integer division variables "expanded"
2730 * into info->tab and info->bmap and
2731 * update info->ineq with respect to the redundant constraints
2732 * in the resulting tableau.
2733 * "bmap" contains the result of this insertion in info->bmap,
2734 * while info->bmap is the original version
2735 * of "bmap", i.e., the one that corresponds to the current
2736 * state of info->tab. The number of constraints in info->bmap
2737 * is assumed to be the same as the number of constraints
2738 * in info->tab. This is required to be able to detect
2739 * the extra constraints in "bmap".
2741 * In particular, introduce extra variables corresponding
2742 * to the extra integer divisions and add the div constraints
2743 * that were added to "bmap" after info->tab was created
2744 * from info->bmap.
2745 * Furthermore, check if these extra integer divisions happen
2746 * to attain a fixed integer value in info->tab.
2747 * If so, replace the corresponding div constraints by pairs
2748 * of inequality constraints that fix these
2749 * integer divisions to their single integer values.
2750 * Replace info->bmap by "bmap" to match the changes to info->tab.
2751 * info->ineq was computed without a tableau and therefore
2752 * does not take into account the redundant constraints
2753 * in the tableau. Mark them here.
2754 * There is no need to check the newly added div constraints
2755 * since they cannot be redundant.
2756 * The redundancy check is not performed when constants have been discovered
2757 * since info->ineq is completely thrown away in this case.
2759 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2760 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2762 int i, n_ineq;
2763 unsigned n_eq;
2764 struct isl_tab_undo *snap;
2765 int any;
2767 if (!bmap)
2768 return isl_stat_error;
2769 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2770 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2771 "original tableau does not correspond "
2772 "to original basic map", goto error);
2774 if (isl_tab_extend_vars(info->tab, n) < 0)
2775 goto error;
2776 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2777 goto error;
2779 for (i = 0; i < n; ++i) {
2780 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2781 goto error;
2784 snap = isl_tab_snap(info->tab);
2786 n_ineq = info->tab->n_con - info->tab->n_eq;
2787 if (copy_ineq(info->tab, bmap) < 0)
2788 goto error;
2790 isl_basic_map_free(info->bmap);
2791 info->bmap = bmap;
2793 any = 0;
2794 for (i = 0; i < n; ++i) {
2795 expanded[i].cst = isl_tab_is_constant(info->tab,
2796 expanded[i].pos, &expanded[i].val);
2797 if (expanded[i].cst < 0)
2798 return isl_stat_error;
2799 if (expanded[i].cst)
2800 any = 1;
2803 if (any) {
2804 if (isl_tab_rollback(info->tab, snap) < 0)
2805 return isl_stat_error;
2806 info->bmap = isl_basic_map_cow(info->bmap);
2807 if (isl_basic_map_free_inequality(info->bmap, 2 * n) < 0)
2808 return isl_stat_error;
2810 return fix_constant_divs(info, n, expanded);
2813 n_eq = info->bmap->n_eq;
2814 for (i = 0; i < n_ineq; ++i) {
2815 if (isl_tab_is_redundant(info->tab, n_eq + i))
2816 info->ineq[i] = STATUS_REDUNDANT;
2819 return isl_stat_ok;
2820 error:
2821 isl_basic_map_free(bmap);
2822 return isl_stat_error;
2825 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2826 * in isl_basic_map_expand_divs using the expansion "exp" and
2827 * update info->ineq with respect to the redundant constraints
2828 * in the resulting tableau. info->bmap is the original version
2829 * of "bmap", i.e., the one that corresponds to the current
2830 * state of info->tab. The number of constraints in info->bmap
2831 * is assumed to be the same as the number of constraints
2832 * in info->tab. This is required to be able to detect
2833 * the extra constraints in "bmap".
2835 * Extract the positions where extra local variables are introduced
2836 * from "exp" and call tab_insert_divs.
2838 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2839 __isl_take isl_basic_map *bmap)
2841 isl_ctx *ctx;
2842 struct isl_expanded *expanded;
2843 int i, j, k, n;
2844 int extra_var;
2845 unsigned total, pos, n_div;
2846 isl_stat r;
2848 total = isl_basic_map_dim(bmap, isl_dim_all);
2849 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2850 pos = total - n_div;
2851 extra_var = total - info->tab->n_var;
2852 n = n_div - extra_var;
2854 ctx = isl_basic_map_get_ctx(bmap);
2855 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
2856 if (extra_var && !expanded)
2857 goto error;
2859 i = 0;
2860 k = 0;
2861 for (j = 0; j < n_div; ++j) {
2862 if (i < n && exp[i] == j) {
2863 ++i;
2864 continue;
2866 expanded[k++].pos = pos + j;
2869 for (k = 0; k < extra_var; ++k)
2870 isl_int_init(expanded[k].val);
2872 r = tab_insert_divs(info, extra_var, expanded, bmap);
2874 for (k = 0; k < extra_var; ++k)
2875 isl_int_clear(expanded[k].val);
2876 free(expanded);
2878 return r;
2879 error:
2880 isl_basic_map_free(bmap);
2881 return isl_stat_error;
2884 /* Check if the union of the basic maps represented by info[i] and info[j]
2885 * can be represented by a single basic map,
2886 * after expanding the divs of info[i] to match those of info[j].
2887 * If so, replace the pair by the single basic map and return
2888 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2889 * Otherwise, return isl_change_none.
2891 * The caller has already checked for info[j] being a subset of info[i].
2892 * If some of the divs of info[j] are unknown, then the expanded info[i]
2893 * will not have the corresponding div constraints. The other patterns
2894 * therefore cannot apply. Skip the computation in this case.
2896 * The expansion is performed using the divs "div" and expansion "exp"
2897 * computed by the caller.
2898 * info[i].bmap has already been expanded and the result is passed in
2899 * as "bmap".
2900 * The "eq" and "ineq" fields of info[i] reflect the status of
2901 * the constraints of the expanded "bmap" with respect to info[j].tab.
2902 * However, inequality constraints that are redundant in info[i].tab
2903 * have not yet been marked as such because no tableau was available.
2905 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2906 * updating info[i].ineq with respect to the redundant constraints.
2907 * Then try and coalesce the expanded info[i] with info[j],
2908 * reusing the information in info[i].eq and info[i].ineq.
2909 * If this does not result in any coalescing or if it results in info[j]
2910 * getting dropped (which should not happen in practice, since the case
2911 * of info[j] being a subset of info[i] has already been checked by
2912 * the caller), then revert info[i] to its original state.
2914 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2915 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2916 int *exp)
2918 isl_bool known;
2919 isl_basic_map *bmap_i;
2920 struct isl_tab_undo *snap;
2921 enum isl_change change = isl_change_none;
2923 known = isl_basic_map_divs_known(info[j].bmap);
2924 if (known < 0 || !known) {
2925 clear_status(&info[i]);
2926 isl_basic_map_free(bmap);
2927 return known < 0 ? isl_change_error : isl_change_none;
2930 bmap_i = isl_basic_map_copy(info[i].bmap);
2931 snap = isl_tab_snap(info[i].tab);
2932 if (expand_tab(&info[i], exp, bmap) < 0)
2933 change = isl_change_error;
2935 init_status(&info[j]);
2936 if (change == isl_change_none)
2937 change = coalesce_local_pair_reuse(i, j, info);
2938 else
2939 clear_status(&info[i]);
2940 if (change != isl_change_none && change != isl_change_drop_second) {
2941 isl_basic_map_free(bmap_i);
2942 } else {
2943 isl_basic_map_free(info[i].bmap);
2944 info[i].bmap = bmap_i;
2946 if (isl_tab_rollback(info[i].tab, snap) < 0)
2947 change = isl_change_error;
2950 return change;
2953 /* Check if the union of "bmap" and the basic map represented by info[j]
2954 * can be represented by a single basic map,
2955 * after expanding the divs of "bmap" to match those of info[j].
2956 * If so, replace the pair by the single basic map and return
2957 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2958 * Otherwise, return isl_change_none.
2960 * In particular, check if the expanded "bmap" contains the basic map
2961 * represented by the tableau info[j].tab.
2962 * The expansion is performed using the divs "div" and expansion "exp"
2963 * computed by the caller.
2964 * Then we check if all constraints of the expanded "bmap" are valid for
2965 * info[j].tab.
2967 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2968 * In this case, the positions of the constraints of info[i].bmap
2969 * with respect to the basic map represented by info[j] are stored
2970 * in info[i].
2972 * If the expanded "bmap" does not contain the basic map
2973 * represented by the tableau info[j].tab and if "i" is not -1,
2974 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2975 * as well and check if that results in coalescing.
2977 static enum isl_change coalesce_with_expanded_divs(
2978 __isl_keep isl_basic_map *bmap, int i, int j,
2979 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2981 enum isl_change change = isl_change_none;
2982 struct isl_coalesce_info info_local, *info_i;
2984 info_i = i >= 0 ? &info[i] : &info_local;
2985 init_status(info_i);
2986 bmap = isl_basic_map_copy(bmap);
2987 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2988 bmap = isl_basic_map_mark_final(bmap);
2990 if (!bmap)
2991 goto error;
2993 info_local.bmap = bmap;
2994 info_i->eq = eq_status_in(bmap, info[j].tab);
2995 if (bmap->n_eq && !info_i->eq)
2996 goto error;
2997 if (any_eq(info_i, STATUS_ERROR))
2998 goto error;
2999 if (any_eq(info_i, STATUS_SEPARATE))
3000 goto done;
3002 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
3003 if (bmap->n_ineq && !info_i->ineq)
3004 goto error;
3005 if (any_ineq(info_i, STATUS_ERROR))
3006 goto error;
3007 if (any_ineq(info_i, STATUS_SEPARATE))
3008 goto done;
3010 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
3011 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
3012 drop(&info[j]);
3013 change = isl_change_drop_second;
3016 if (change == isl_change_none && i != -1)
3017 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
3019 done:
3020 isl_basic_map_free(bmap);
3021 clear_status(info_i);
3022 return change;
3023 error:
3024 isl_basic_map_free(bmap);
3025 clear_status(info_i);
3026 return isl_change_error;
3029 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3030 * can be represented by a single basic map,
3031 * after aligning the divs of "bmap_i" to match those of info[j].
3032 * If so, replace the pair by the single basic map and return
3033 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3034 * Otherwise, return isl_change_none.
3036 * In particular, check if "bmap_i" contains the basic map represented by
3037 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3038 * Note that this can only succeed if the number of divs of "bmap_i"
3039 * is smaller than (or equal to) the number of divs of info[j].
3041 * We first check if the divs of "bmap_i" are all known and form a subset
3042 * of those of info[j].bmap. If so, we pass control over to
3043 * coalesce_with_expanded_divs.
3045 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3047 static enum isl_change coalesce_after_aligning_divs(
3048 __isl_keep isl_basic_map *bmap_i, int i, int j,
3049 struct isl_coalesce_info *info)
3051 int known;
3052 isl_mat *div_i, *div_j, *div;
3053 int *exp1 = NULL;
3054 int *exp2 = NULL;
3055 isl_ctx *ctx;
3056 enum isl_change change;
3058 known = isl_basic_map_divs_known(bmap_i);
3059 if (known < 0 || !known)
3060 return known;
3062 ctx = isl_basic_map_get_ctx(bmap_i);
3064 div_i = isl_basic_map_get_divs(bmap_i);
3065 div_j = isl_basic_map_get_divs(info[j].bmap);
3067 if (!div_i || !div_j)
3068 goto error;
3070 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3071 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3072 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
3073 goto error;
3075 div = isl_merge_divs(div_i, div_j, exp1, exp2);
3076 if (!div)
3077 goto error;
3079 if (div->n_row == div_j->n_row)
3080 change = coalesce_with_expanded_divs(bmap_i,
3081 i, j, info, div, exp1);
3082 else
3083 change = isl_change_none;
3085 isl_mat_free(div);
3087 isl_mat_free(div_i);
3088 isl_mat_free(div_j);
3090 free(exp2);
3091 free(exp1);
3093 return change;
3094 error:
3095 isl_mat_free(div_i);
3096 isl_mat_free(div_j);
3097 free(exp1);
3098 free(exp2);
3099 return isl_change_error;
3102 /* Check if basic map "j" is a subset of basic map "i" after
3103 * exploiting the extra equalities of "j" to simplify the divs of "i".
3104 * If so, remove basic map "j" and return isl_change_drop_second.
3106 * If "j" does not have any equalities or if they are the same
3107 * as those of "i", then we cannot exploit them to simplify the divs.
3108 * Similarly, if there are no divs in "i", then they cannot be simplified.
3109 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3110 * then "j" cannot be a subset of "i".
3112 * Otherwise, we intersect "i" with the affine hull of "j" and then
3113 * check if "j" is a subset of the result after aligning the divs.
3114 * If so, then "j" is definitely a subset of "i" and can be removed.
3115 * Note that if after intersection with the affine hull of "j".
3116 * "i" still has more divs than "j", then there is no way we can
3117 * align the divs of "i" to those of "j".
3119 static enum isl_change coalesce_subset_with_equalities(int i, int j,
3120 struct isl_coalesce_info *info)
3122 isl_basic_map *hull_i, *hull_j, *bmap_i;
3123 int equal, empty;
3124 enum isl_change change;
3126 if (info[j].bmap->n_eq == 0)
3127 return isl_change_none;
3128 if (info[i].bmap->n_div == 0)
3129 return isl_change_none;
3131 hull_i = isl_basic_map_copy(info[i].bmap);
3132 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3133 hull_j = isl_basic_map_copy(info[j].bmap);
3134 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3136 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3137 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3138 empty = isl_basic_map_plain_is_empty(hull_j);
3139 isl_basic_map_free(hull_i);
3141 if (equal < 0 || equal || empty < 0 || empty) {
3142 isl_basic_map_free(hull_j);
3143 if (equal < 0 || empty < 0)
3144 return isl_change_error;
3145 return isl_change_none;
3148 bmap_i = isl_basic_map_copy(info[i].bmap);
3149 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3150 if (!bmap_i)
3151 return isl_change_error;
3153 if (bmap_i->n_div > info[j].bmap->n_div) {
3154 isl_basic_map_free(bmap_i);
3155 return isl_change_none;
3158 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3160 isl_basic_map_free(bmap_i);
3162 return change;
3165 /* Check if the union of and the basic maps represented by info[i] and info[j]
3166 * can be represented by a single basic map, by aligning or equating
3167 * their integer divisions.
3168 * If so, replace the pair by the single basic map and return
3169 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3170 * Otherwise, return isl_change_none.
3172 * Note that we only perform any test if the number of divs is different
3173 * in the two basic maps. In case the number of divs is the same,
3174 * we have already established that the divs are different
3175 * in the two basic maps.
3176 * In particular, if the number of divs of basic map i is smaller than
3177 * the number of divs of basic map j, then we check if j is a subset of i
3178 * and vice versa.
3180 static enum isl_change coalesce_divs(int i, int j,
3181 struct isl_coalesce_info *info)
3183 enum isl_change change = isl_change_none;
3185 if (info[i].bmap->n_div < info[j].bmap->n_div)
3186 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3187 if (change != isl_change_none)
3188 return change;
3190 if (info[j].bmap->n_div < info[i].bmap->n_div)
3191 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3192 if (change != isl_change_none)
3193 return invert_change(change);
3195 change = coalesce_subset_with_equalities(i, j, info);
3196 if (change != isl_change_none)
3197 return change;
3199 change = coalesce_subset_with_equalities(j, i, info);
3200 if (change != isl_change_none)
3201 return invert_change(change);
3203 return isl_change_none;
3206 /* Does "bmap" involve any divs that themselves refer to divs?
3208 static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
3210 int i;
3211 unsigned total;
3212 unsigned n_div;
3214 total = isl_basic_map_dim(bmap, isl_dim_all);
3215 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3216 total -= n_div;
3218 for (i = 0; i < n_div; ++i)
3219 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
3220 n_div) != -1)
3221 return isl_bool_true;
3223 return isl_bool_false;
3226 /* Return a list of affine expressions, one for each integer division
3227 * in "bmap_i". For each integer division that also appears in "bmap_j",
3228 * the affine expression is set to NaN. The number of NaNs in the list
3229 * is equal to the number of integer divisions in "bmap_j".
3230 * For the other integer divisions of "bmap_i", the corresponding
3231 * element in the list is a purely affine expression equal to the integer
3232 * division in "hull".
3233 * If no such list can be constructed, then the number of elements
3234 * in the returned list is smaller than the number of integer divisions
3235 * in "bmap_i".
3237 static __isl_give isl_aff_list *set_up_substitutions(
3238 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3239 __isl_take isl_basic_map *hull)
3241 unsigned n_div_i, n_div_j, total;
3242 isl_ctx *ctx;
3243 isl_local_space *ls;
3244 isl_basic_set *wrap_hull;
3245 isl_aff *aff_nan;
3246 isl_aff_list *list;
3247 int i, j;
3249 if (!hull)
3250 return NULL;
3252 ctx = isl_basic_map_get_ctx(hull);
3254 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3255 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3256 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
3258 ls = isl_basic_map_get_local_space(bmap_i);
3259 ls = isl_local_space_wrap(ls);
3260 wrap_hull = isl_basic_map_wrap(hull);
3262 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3263 list = isl_aff_list_alloc(ctx, n_div_i);
3265 j = 0;
3266 for (i = 0; i < n_div_i; ++i) {
3267 isl_aff *aff;
3269 if (j < n_div_j &&
3270 isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3271 0, 2 + total)) {
3272 ++j;
3273 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3274 continue;
3276 if (n_div_i - i <= n_div_j - j)
3277 break;
3279 aff = isl_local_space_get_div(ls, i);
3280 aff = isl_aff_substitute_equalities(aff,
3281 isl_basic_set_copy(wrap_hull));
3282 aff = isl_aff_floor(aff);
3283 if (!aff)
3284 goto error;
3285 if (isl_aff_dim(aff, isl_dim_div) != 0) {
3286 isl_aff_free(aff);
3287 break;
3290 list = isl_aff_list_add(list, aff);
3293 isl_aff_free(aff_nan);
3294 isl_local_space_free(ls);
3295 isl_basic_set_free(wrap_hull);
3297 return list;
3298 error:
3299 isl_aff_free(aff_nan);
3300 isl_local_space_free(ls);
3301 isl_basic_set_free(wrap_hull);
3302 isl_aff_list_free(list);
3303 return NULL;
3306 /* Add variables to info->bmap and info->tab corresponding to the elements
3307 * in "list" that are not set to NaN.
3308 * "extra_var" is the number of these elements.
3309 * "dim" is the offset in the variables of "tab" where we should
3310 * start considering the elements in "list".
3311 * When this function returns, the total number of variables in "tab"
3312 * is equal to "dim" plus the number of elements in "list".
3314 * The newly added existentially quantified variables are not given
3315 * an explicit representation because the corresponding div constraints
3316 * do not appear in info->bmap. These constraints are not added
3317 * to info->bmap because for internal consistency, they would need to
3318 * be added to info->tab as well, where they could combine with the equality
3319 * that is added later to result in constraints that do not hold
3320 * in the original input.
3322 static isl_stat add_sub_vars(struct isl_coalesce_info *info,
3323 __isl_keep isl_aff_list *list, int dim, int extra_var)
3325 int i, j, n, d;
3326 isl_space *space;
3328 space = isl_basic_map_get_space(info->bmap);
3329 info->bmap = isl_basic_map_cow(info->bmap);
3330 info->bmap = isl_basic_map_extend_space(info->bmap, space,
3331 extra_var, 0, 0);
3332 if (!info->bmap)
3333 return isl_stat_error;
3334 n = isl_aff_list_n_aff(list);
3335 for (i = 0; i < n; ++i) {
3336 int is_nan;
3337 isl_aff *aff;
3339 aff = isl_aff_list_get_aff(list, i);
3340 is_nan = isl_aff_is_nan(aff);
3341 isl_aff_free(aff);
3342 if (is_nan < 0)
3343 return isl_stat_error;
3344 if (is_nan)
3345 continue;
3347 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3348 return isl_stat_error;
3349 d = isl_basic_map_alloc_div(info->bmap);
3350 if (d < 0)
3351 return isl_stat_error;
3352 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3353 if (!info->bmap)
3354 return isl_stat_error;
3355 for (j = d; j > i; --j)
3356 isl_basic_map_swap_div(info->bmap, j - 1, j);
3359 return isl_stat_ok;
3362 /* For each element in "list" that is not set to NaN, fix the corresponding
3363 * variable in "tab" to the purely affine expression defined by the element.
3364 * "dim" is the offset in the variables of "tab" where we should
3365 * start considering the elements in "list".
3367 * This function assumes that a sufficient number of rows and
3368 * elements in the constraint array are available in the tableau.
3370 static int add_sub_equalities(struct isl_tab *tab,
3371 __isl_keep isl_aff_list *list, int dim)
3373 int i, n;
3374 isl_ctx *ctx;
3375 isl_vec *sub;
3376 isl_aff *aff;
3378 n = isl_aff_list_n_aff(list);
3380 ctx = isl_tab_get_ctx(tab);
3381 sub = isl_vec_alloc(ctx, 1 + dim + n);
3382 if (!sub)
3383 return -1;
3384 isl_seq_clr(sub->el + 1 + dim, n);
3386 for (i = 0; i < n; ++i) {
3387 aff = isl_aff_list_get_aff(list, i);
3388 if (!aff)
3389 goto error;
3390 if (isl_aff_is_nan(aff)) {
3391 isl_aff_free(aff);
3392 continue;
3394 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3395 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3396 if (isl_tab_add_eq(tab, sub->el) < 0)
3397 goto error;
3398 isl_int_set_si(sub->el[1 + dim + i], 0);
3399 isl_aff_free(aff);
3402 isl_vec_free(sub);
3403 return 0;
3404 error:
3405 isl_aff_free(aff);
3406 isl_vec_free(sub);
3407 return -1;
3410 /* Add variables to info->tab and info->bmap corresponding to the elements
3411 * in "list" that are not set to NaN. The value of the added variable
3412 * in info->tab is fixed to the purely affine expression defined by the element.
3413 * "dim" is the offset in the variables of info->tab where we should
3414 * start considering the elements in "list".
3415 * When this function returns, the total number of variables in info->tab
3416 * is equal to "dim" plus the number of elements in "list".
3418 static int add_subs(struct isl_coalesce_info *info,
3419 __isl_keep isl_aff_list *list, int dim)
3421 int extra_var;
3422 int n;
3424 if (!list)
3425 return -1;
3427 n = isl_aff_list_n_aff(list);
3428 extra_var = n - (info->tab->n_var - dim);
3430 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3431 return -1;
3432 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3433 return -1;
3434 if (add_sub_vars(info, list, dim, extra_var) < 0)
3435 return -1;
3437 return add_sub_equalities(info->tab, list, dim);
3440 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3441 * divisions in "i" but not in "j" to basic map "j", with values
3442 * specified by "list". The total number of elements in "list"
3443 * is equal to the number of integer divisions in "i", while the number
3444 * of NaN elements in the list is equal to the number of integer divisions
3445 * in "j".
3447 * If no coalescing can be performed, then we need to revert basic map "j"
3448 * to its original state. We do the same if basic map "i" gets dropped
3449 * during the coalescing, even though this should not happen in practice
3450 * since we have already checked for "j" being a subset of "i"
3451 * before we reach this stage.
3453 static enum isl_change coalesce_with_subs(int i, int j,
3454 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3456 isl_basic_map *bmap_j;
3457 struct isl_tab_undo *snap;
3458 unsigned dim;
3459 enum isl_change change;
3461 bmap_j = isl_basic_map_copy(info[j].bmap);
3462 snap = isl_tab_snap(info[j].tab);
3464 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3465 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
3466 if (add_subs(&info[j], list, dim) < 0)
3467 goto error;
3469 change = coalesce_local_pair(i, j, info);
3470 if (change != isl_change_none && change != isl_change_drop_first) {
3471 isl_basic_map_free(bmap_j);
3472 } else {
3473 isl_basic_map_free(info[j].bmap);
3474 info[j].bmap = bmap_j;
3476 if (isl_tab_rollback(info[j].tab, snap) < 0)
3477 return isl_change_error;
3480 return change;
3481 error:
3482 isl_basic_map_free(bmap_j);
3483 return isl_change_error;
3486 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3487 * those extra integer divisions in "i" that can be simplified away
3488 * using the extra equalities in "j".
3489 * All divs are assumed to be known and not contain any nested divs.
3491 * We first check if there are any extra equalities in "j" that we
3492 * can exploit. Then we check if every integer division in "i"
3493 * either already appears in "j" or can be simplified using the
3494 * extra equalities to a purely affine expression.
3495 * If these tests succeed, then we try to coalesce the two basic maps
3496 * by introducing extra dimensions in "j" corresponding to
3497 * the extra integer divsisions "i" fixed to the corresponding
3498 * purely affine expression.
3500 static enum isl_change check_coalesce_into_eq(int i, int j,
3501 struct isl_coalesce_info *info)
3503 unsigned n_div_i, n_div_j;
3504 isl_basic_map *hull_i, *hull_j;
3505 int equal, empty;
3506 isl_aff_list *list;
3507 enum isl_change change;
3509 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3510 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3511 if (n_div_i <= n_div_j)
3512 return isl_change_none;
3513 if (info[j].bmap->n_eq == 0)
3514 return isl_change_none;
3516 hull_i = isl_basic_map_copy(info[i].bmap);
3517 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3518 hull_j = isl_basic_map_copy(info[j].bmap);
3519 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3521 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3522 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3523 empty = isl_basic_map_plain_is_empty(hull_j);
3524 isl_basic_map_free(hull_i);
3526 if (equal < 0 || empty < 0)
3527 goto error;
3528 if (equal || empty) {
3529 isl_basic_map_free(hull_j);
3530 return isl_change_none;
3533 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3534 if (!list)
3535 return isl_change_error;
3536 if (isl_aff_list_n_aff(list) < n_div_i)
3537 change = isl_change_none;
3538 else
3539 change = coalesce_with_subs(i, j, info, list);
3541 isl_aff_list_free(list);
3543 return change;
3544 error:
3545 isl_basic_map_free(hull_j);
3546 return isl_change_error;
3549 /* Check if we can coalesce basic maps "i" and "j" after copying
3550 * those extra integer divisions in one of the basic maps that can
3551 * be simplified away using the extra equalities in the other basic map.
3552 * We require all divs to be known in both basic maps.
3553 * Furthermore, to simplify the comparison of div expressions,
3554 * we do not allow any nested integer divisions.
3556 static enum isl_change check_coalesce_eq(int i, int j,
3557 struct isl_coalesce_info *info)
3559 isl_bool known, nested;
3560 enum isl_change change;
3562 known = isl_basic_map_divs_known(info[i].bmap);
3563 if (known < 0 || !known)
3564 return known < 0 ? isl_change_error : isl_change_none;
3565 known = isl_basic_map_divs_known(info[j].bmap);
3566 if (known < 0 || !known)
3567 return known < 0 ? isl_change_error : isl_change_none;
3568 nested = has_nested_div(info[i].bmap);
3569 if (nested < 0 || nested)
3570 return nested < 0 ? isl_change_error : isl_change_none;
3571 nested = has_nested_div(info[j].bmap);
3572 if (nested < 0 || nested)
3573 return nested < 0 ? isl_change_error : isl_change_none;
3575 change = check_coalesce_into_eq(i, j, info);
3576 if (change != isl_change_none)
3577 return change;
3578 change = check_coalesce_into_eq(j, i, info);
3579 if (change != isl_change_none)
3580 return invert_change(change);
3582 return isl_change_none;
3585 /* Check if the union of the given pair of basic maps
3586 * can be represented by a single basic map.
3587 * If so, replace the pair by the single basic map and return
3588 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3589 * Otherwise, return isl_change_none.
3591 * We first check if the two basic maps live in the same local space,
3592 * after aligning the divs that differ by only an integer constant.
3593 * If so, we do the complete check. Otherwise, we check if they have
3594 * the same number of integer divisions and can be coalesced, if one is
3595 * an obvious subset of the other or if the extra integer divisions
3596 * of one basic map can be simplified away using the extra equalities
3597 * of the other basic map.
3599 static enum isl_change coalesce_pair(int i, int j,
3600 struct isl_coalesce_info *info)
3602 isl_bool same;
3603 enum isl_change change;
3605 if (harmonize_divs(&info[i], &info[j]) < 0)
3606 return isl_change_error;
3607 same = same_divs(info[i].bmap, info[j].bmap);
3608 if (same < 0)
3609 return isl_change_error;
3610 if (same)
3611 return coalesce_local_pair(i, j, info);
3613 if (info[i].bmap->n_div == info[j].bmap->n_div) {
3614 change = coalesce_local_pair(i, j, info);
3615 if (change != isl_change_none)
3616 return change;
3619 change = coalesce_divs(i, j, info);
3620 if (change != isl_change_none)
3621 return change;
3623 return check_coalesce_eq(i, j, info);
3626 /* Return the maximum of "a" and "b".
3628 static int isl_max(int a, int b)
3630 return a > b ? a : b;
3633 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3634 * with those in the range [start2, end2[, skipping basic maps
3635 * that have been removed (either before or within this function).
3637 * For each basic map i in the first range, we check if it can be coalesced
3638 * with respect to any previously considered basic map j in the second range.
3639 * If i gets dropped (because it was a subset of some j), then
3640 * we can move on to the next basic map.
3641 * If j gets dropped, we need to continue checking against the other
3642 * previously considered basic maps.
3643 * If the two basic maps got fused, then we recheck the fused basic map
3644 * against the previously considered basic maps, starting at i + 1
3645 * (even if start2 is greater than i + 1).
3647 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3648 int start1, int end1, int start2, int end2)
3650 int i, j;
3652 for (i = end1 - 1; i >= start1; --i) {
3653 if (info[i].removed)
3654 continue;
3655 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3656 enum isl_change changed;
3658 if (info[j].removed)
3659 continue;
3660 if (info[i].removed)
3661 isl_die(ctx, isl_error_internal,
3662 "basic map unexpectedly removed",
3663 return -1);
3664 changed = coalesce_pair(i, j, info);
3665 switch (changed) {
3666 case isl_change_error:
3667 return -1;
3668 case isl_change_none:
3669 case isl_change_drop_second:
3670 continue;
3671 case isl_change_drop_first:
3672 j = end2;
3673 break;
3674 case isl_change_fuse:
3675 j = i;
3676 break;
3681 return 0;
3684 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3686 * We consider groups of basic maps that live in the same apparent
3687 * affine hull and we first coalesce within such a group before we
3688 * coalesce the elements in the group with elements of previously
3689 * considered groups. If a fuse happens during the second phase,
3690 * then we also reconsider the elements within the group.
3692 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3694 int start, end;
3696 for (end = n; end > 0; end = start) {
3697 start = end - 1;
3698 while (start >= 1 &&
3699 info[start - 1].hull_hash == info[start].hull_hash)
3700 start--;
3701 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3702 return -1;
3703 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3704 return -1;
3707 return 0;
3710 /* Update the basic maps in "map" based on the information in "info".
3711 * In particular, remove the basic maps that have been marked removed and
3712 * update the others based on the information in the corresponding tableau.
3713 * Since we detected implicit equalities without calling
3714 * isl_basic_map_gauss, we need to do it now.
3715 * Also call isl_basic_map_simplify if we may have lost the definition
3716 * of one or more integer divisions.
3718 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3719 int n, struct isl_coalesce_info *info)
3721 int i;
3723 if (!map)
3724 return NULL;
3726 for (i = n - 1; i >= 0; --i) {
3727 if (info[i].removed) {
3728 isl_basic_map_free(map->p[i]);
3729 if (i != map->n - 1)
3730 map->p[i] = map->p[map->n - 1];
3731 map->n--;
3732 continue;
3735 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3736 info[i].tab);
3737 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3738 if (info[i].simplify)
3739 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3740 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3741 if (!info[i].bmap)
3742 return isl_map_free(map);
3743 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3744 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3745 isl_basic_map_free(map->p[i]);
3746 map->p[i] = info[i].bmap;
3747 info[i].bmap = NULL;
3750 return map;
3753 /* For each pair of basic maps in the map, check if the union of the two
3754 * can be represented by a single basic map.
3755 * If so, replace the pair by the single basic map and start over.
3757 * We factor out any (hidden) common factor from the constraint
3758 * coefficients to improve the detection of adjacent constraints.
3760 * Since we are constructing the tableaus of the basic maps anyway,
3761 * we exploit them to detect implicit equalities and redundant constraints.
3762 * This also helps the coalescing as it can ignore the redundant constraints.
3763 * In order to avoid confusion, we make all implicit equalities explicit
3764 * in the basic maps. We don't call isl_basic_map_gauss, though,
3765 * as that may affect the number of constraints.
3766 * This means that we have to call isl_basic_map_gauss at the end
3767 * of the computation (in update_basic_maps) to ensure that
3768 * the basic maps are not left in an unexpected state.
3769 * For each basic map, we also compute the hash of the apparent affine hull
3770 * for use in coalesce.
3772 __isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
3774 int i;
3775 unsigned n;
3776 isl_ctx *ctx;
3777 struct isl_coalesce_info *info = NULL;
3779 map = isl_map_remove_empty_parts(map);
3780 if (!map)
3781 return NULL;
3783 if (map->n <= 1)
3784 return map;
3786 ctx = isl_map_get_ctx(map);
3787 map = isl_map_sort_divs(map);
3788 map = isl_map_cow(map);
3790 if (!map)
3791 return NULL;
3793 n = map->n;
3795 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3796 if (!info)
3797 goto error;
3799 for (i = 0; i < map->n; ++i) {
3800 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3801 if (!map->p[i])
3802 goto error;
3803 info[i].bmap = isl_basic_map_copy(map->p[i]);
3804 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3805 if (!info[i].tab)
3806 goto error;
3807 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3808 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3809 goto error;
3810 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3811 info[i].bmap);
3812 if (!info[i].bmap)
3813 goto error;
3814 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3815 if (isl_tab_detect_redundant(info[i].tab) < 0)
3816 goto error;
3817 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3818 goto error;
3820 for (i = map->n - 1; i >= 0; --i)
3821 if (info[i].tab->empty)
3822 drop(&info[i]);
3824 if (coalesce(ctx, n, info) < 0)
3825 goto error;
3827 map = update_basic_maps(map, n, info);
3829 clear_coalesce_info(n, info);
3831 return map;
3832 error:
3833 clear_coalesce_info(n, info);
3834 isl_map_free(map);
3835 return NULL;
3838 /* For each pair of basic sets in the set, check if the union of the two
3839 * can be represented by a single basic set.
3840 * If so, replace the pair by the single basic set and start over.
3842 struct isl_set *isl_set_coalesce(struct isl_set *set)
3844 return set_from_map(isl_map_coalesce(set_to_map(set)));