isl_map_simplify.c: fix typos in comments
[isl.git] / isl_coalesce.c
blob476f8154981cd2ceebec0b4810d1ca2293395c5a
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 /* Are all non-redundant constraints of the basic map represented by "info"
200 * either valid or cut constraints with respect to the other basic map?
202 static int all_valid_or_cut(struct isl_coalesce_info *info)
204 int i;
206 for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
207 if (info->eq[i] == STATUS_REDUNDANT)
208 continue;
209 if (info->eq[i] == STATUS_VALID)
210 continue;
211 if (info->eq[i] == STATUS_CUT)
212 continue;
213 return 0;
216 for (i = 0; i < info->bmap->n_ineq; ++i) {
217 if (info->ineq[i] == STATUS_REDUNDANT)
218 continue;
219 if (info->ineq[i] == STATUS_VALID)
220 continue;
221 if (info->ineq[i] == STATUS_CUT)
222 continue;
223 return 0;
226 return 1;
229 /* Compute the hash of the (apparent) affine hull of info->bmap (with
230 * the existentially quantified variables removed) and store it
231 * in info->hash.
233 static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
235 isl_basic_map *hull;
236 unsigned n_div;
238 hull = isl_basic_map_copy(info->bmap);
239 hull = isl_basic_map_plain_affine_hull(hull);
240 n_div = isl_basic_map_dim(hull, isl_dim_div);
241 hull = isl_basic_map_drop_constraints_involving_dims(hull,
242 isl_dim_div, 0, n_div);
243 info->hull_hash = isl_basic_map_get_hash(hull);
244 isl_basic_map_free(hull);
246 return hull ? 0 : -1;
249 /* Free all the allocated memory in an array
250 * of "n" isl_coalesce_info elements.
252 static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
254 int i;
256 if (!info)
257 return;
259 for (i = 0; i < n; ++i) {
260 isl_basic_map_free(info[i].bmap);
261 isl_tab_free(info[i].tab);
264 free(info);
267 /* Drop the basic map represented by "info".
268 * That is, clear the memory associated to the entry and
269 * mark it as having been removed.
271 static void drop(struct isl_coalesce_info *info)
273 info->bmap = isl_basic_map_free(info->bmap);
274 isl_tab_free(info->tab);
275 info->tab = NULL;
276 info->removed = 1;
279 /* Exchange the information in "info1" with that in "info2".
281 static void exchange(struct isl_coalesce_info *info1,
282 struct isl_coalesce_info *info2)
284 struct isl_coalesce_info info;
286 info = *info1;
287 *info1 = *info2;
288 *info2 = info;
291 /* This type represents the kind of change that has been performed
292 * while trying to coalesce two basic maps.
294 * isl_change_none: nothing was changed
295 * isl_change_drop_first: the first basic map was removed
296 * isl_change_drop_second: the second basic map was removed
297 * isl_change_fuse: the two basic maps were replaced by a new basic map.
299 enum isl_change {
300 isl_change_error = -1,
301 isl_change_none = 0,
302 isl_change_drop_first,
303 isl_change_drop_second,
304 isl_change_fuse,
307 /* Update "change" based on an interchange of the first and the second
308 * basic map. That is, interchange isl_change_drop_first and
309 * isl_change_drop_second.
311 static enum isl_change invert_change(enum isl_change change)
313 switch (change) {
314 case isl_change_error:
315 return isl_change_error;
316 case isl_change_none:
317 return isl_change_none;
318 case isl_change_drop_first:
319 return isl_change_drop_second;
320 case isl_change_drop_second:
321 return isl_change_drop_first;
322 case isl_change_fuse:
323 return isl_change_fuse;
326 return isl_change_error;
329 /* Add the valid constraints of the basic map represented by "info"
330 * to "bmap". "len" is the size of the constraints.
331 * If only one of the pair of inequalities that make up an equality
332 * is valid, then add that inequality.
334 static __isl_give isl_basic_map *add_valid_constraints(
335 __isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
336 unsigned len)
338 int k, l;
340 if (!bmap)
341 return NULL;
343 for (k = 0; k < info->bmap->n_eq; ++k) {
344 if (info->eq[2 * k] == STATUS_VALID &&
345 info->eq[2 * k + 1] == STATUS_VALID) {
346 l = isl_basic_map_alloc_equality(bmap);
347 if (l < 0)
348 return isl_basic_map_free(bmap);
349 isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
350 } else if (info->eq[2 * k] == STATUS_VALID) {
351 l = isl_basic_map_alloc_inequality(bmap);
352 if (l < 0)
353 return isl_basic_map_free(bmap);
354 isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
355 } else if (info->eq[2 * k + 1] == STATUS_VALID) {
356 l = isl_basic_map_alloc_inequality(bmap);
357 if (l < 0)
358 return isl_basic_map_free(bmap);
359 isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
363 for (k = 0; k < info->bmap->n_ineq; ++k) {
364 if (info->ineq[k] != STATUS_VALID)
365 continue;
366 l = isl_basic_map_alloc_inequality(bmap);
367 if (l < 0)
368 return isl_basic_map_free(bmap);
369 isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
372 return bmap;
375 /* Is "bmap" defined by a number of (non-redundant) constraints that
376 * is greater than the number of constraints of basic maps i and j combined?
377 * Equalities are counted as two inequalities.
379 static int number_of_constraints_increases(int i, int j,
380 struct isl_coalesce_info *info,
381 __isl_keep isl_basic_map *bmap, struct isl_tab *tab)
383 int k, n_old, n_new;
385 n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
386 n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
388 n_new = 2 * bmap->n_eq;
389 for (k = 0; k < bmap->n_ineq; ++k)
390 if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
391 ++n_new;
393 return n_new > n_old;
396 /* Replace the pair of basic maps i and j by the basic map bounded
397 * by the valid constraints in both basic maps and the constraints
398 * in extra (if not NULL).
399 * Place the fused basic map in the position that is the smallest of i and j.
401 * If "detect_equalities" is set, then look for equalities encoded
402 * as pairs of inequalities.
403 * If "check_number" is set, then the original basic maps are only
404 * replaced if the total number of constraints does not increase.
405 * While the number of integer divisions in the two basic maps
406 * is assumed to be the same, the actual definitions may be different.
407 * We only copy the definition from one of the basic map if it is
408 * the same as that of the other basic map. Otherwise, we mark
409 * the integer division as unknown and simplify the basic map
410 * in an attempt to recover the integer division definition.
412 static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
413 __isl_keep isl_mat *extra, int detect_equalities, int check_number)
415 int k, l;
416 struct isl_basic_map *fused = NULL;
417 struct isl_tab *fused_tab = NULL;
418 unsigned total = isl_basic_map_total_dim(info[i].bmap);
419 unsigned extra_rows = extra ? extra->n_row : 0;
420 unsigned n_eq, n_ineq;
421 int simplify = 0;
423 if (j < i)
424 return fuse(j, i, info, extra, detect_equalities, check_number);
426 n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
427 n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
428 fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
429 info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
430 fused = add_valid_constraints(fused, &info[i], 1 + total);
431 fused = add_valid_constraints(fused, &info[j], 1 + total);
432 if (!fused)
433 goto error;
434 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
435 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
436 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
438 for (k = 0; k < info[i].bmap->n_div; ++k) {
439 int l = isl_basic_map_alloc_div(fused);
440 if (l < 0)
441 goto error;
442 if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
443 1 + 1 + total)) {
444 isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
445 1 + 1 + total);
446 } else {
447 isl_int_set_si(fused->div[l][0], 0);
448 simplify = 1;
452 for (k = 0; k < extra_rows; ++k) {
453 l = isl_basic_map_alloc_inequality(fused);
454 if (l < 0)
455 goto error;
456 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
459 if (detect_equalities)
460 fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
461 fused = isl_basic_map_gauss(fused, NULL);
462 if (simplify || info[j].simplify) {
463 fused = isl_basic_map_simplify(fused);
464 info[i].simplify = 0;
466 fused = isl_basic_map_finalize(fused);
468 fused_tab = isl_tab_from_basic_map(fused, 0);
469 if (isl_tab_detect_redundant(fused_tab) < 0)
470 goto error;
472 if (check_number &&
473 number_of_constraints_increases(i, j, info, fused, fused_tab)) {
474 isl_tab_free(fused_tab);
475 isl_basic_map_free(fused);
476 return isl_change_none;
479 isl_basic_map_free(info[i].bmap);
480 info[i].bmap = fused;
481 isl_tab_free(info[i].tab);
482 info[i].tab = fused_tab;
483 drop(&info[j]);
485 return isl_change_fuse;
486 error:
487 isl_tab_free(fused_tab);
488 isl_basic_map_free(fused);
489 return isl_change_error;
492 /* Given a pair of basic maps i and j such that all constraints are either
493 * "valid" or "cut", check if the facets corresponding to the "cut"
494 * constraints of i lie entirely within basic map j.
495 * If so, replace the pair by the basic map consisting of the valid
496 * constraints in both basic maps.
497 * Checking whether the facet lies entirely within basic map j
498 * is performed by checking whether the constraints of basic map j
499 * are valid for the facet. These tests are performed on a rational
500 * tableau to avoid the theoretical possibility that a constraint
501 * that was considered to be a cut constraint for the entire basic map i
502 * happens to be considered to be a valid constraint for the facet,
503 * even though it cuts off the same rational points.
505 * To see that we are not introducing any extra points, call the
506 * two basic maps A and B and the resulting map U and let x
507 * be an element of U \setminus ( A \cup B ).
508 * A line connecting x with an element of A \cup B meets a facet F
509 * of either A or B. Assume it is a facet of B and let c_1 be
510 * the corresponding facet constraint. We have c_1(x) < 0 and
511 * so c_1 is a cut constraint. This implies that there is some
512 * (possibly rational) point x' satisfying the constraints of A
513 * and the opposite of c_1 as otherwise c_1 would have been marked
514 * valid for A. The line connecting x and x' meets a facet of A
515 * in a (possibly rational) point that also violates c_1, but this
516 * is impossible since all cut constraints of B are valid for all
517 * cut facets of A.
518 * In case F is a facet of A rather than B, then we can apply the
519 * above reasoning to find a facet of B separating x from A \cup B first.
521 static enum isl_change check_facets(int i, int j,
522 struct isl_coalesce_info *info)
524 int k, l;
525 struct isl_tab_undo *snap, *snap2;
526 unsigned n_eq = info[i].bmap->n_eq;
528 snap = isl_tab_snap(info[i].tab);
529 if (isl_tab_mark_rational(info[i].tab) < 0)
530 return isl_change_error;
531 snap2 = isl_tab_snap(info[i].tab);
533 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
534 if (info[i].ineq[k] != STATUS_CUT)
535 continue;
536 if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
537 return isl_change_error;
538 for (l = 0; l < info[j].bmap->n_ineq; ++l) {
539 int stat;
540 if (info[j].ineq[l] != STATUS_CUT)
541 continue;
542 stat = status_in(info[j].bmap->ineq[l], info[i].tab);
543 if (stat < 0)
544 return isl_change_error;
545 if (stat != STATUS_VALID)
546 break;
548 if (isl_tab_rollback(info[i].tab, snap2) < 0)
549 return isl_change_error;
550 if (l < info[j].bmap->n_ineq)
551 break;
554 if (k < info[i].bmap->n_ineq) {
555 if (isl_tab_rollback(info[i].tab, snap) < 0)
556 return isl_change_error;
557 return isl_change_none;
559 return fuse(i, j, info, NULL, 0, 0);
562 /* Check if info->bmap contains the basic map represented
563 * by the tableau "tab".
564 * For each equality, we check both the constraint itself
565 * (as an inequality) and its negation. Make sure the
566 * equality is returned to its original state before returning.
568 static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
570 int k;
571 unsigned dim;
572 isl_basic_map *bmap = info->bmap;
574 dim = isl_basic_map_total_dim(bmap);
575 for (k = 0; k < bmap->n_eq; ++k) {
576 int stat;
577 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
578 stat = status_in(bmap->eq[k], tab);
579 isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
580 if (stat < 0)
581 return isl_bool_error;
582 if (stat != STATUS_VALID)
583 return isl_bool_false;
584 stat = status_in(bmap->eq[k], tab);
585 if (stat < 0)
586 return isl_bool_error;
587 if (stat != STATUS_VALID)
588 return isl_bool_false;
591 for (k = 0; k < bmap->n_ineq; ++k) {
592 int stat;
593 if (info->ineq[k] == STATUS_REDUNDANT)
594 continue;
595 stat = status_in(bmap->ineq[k], tab);
596 if (stat < 0)
597 return isl_bool_error;
598 if (stat != STATUS_VALID)
599 return isl_bool_false;
601 return isl_bool_true;
604 /* Basic map "i" has an inequality (say "k") that is adjacent
605 * to some inequality of basic map "j". All the other inequalities
606 * are valid for "j".
607 * Check if basic map "j" forms an extension of basic map "i".
609 * Note that this function is only called if some of the equalities or
610 * inequalities of basic map "j" do cut basic map "i". The function is
611 * correct even if there are no such cut constraints, but in that case
612 * the additional checks performed by this function are overkill.
614 * In particular, we replace constraint k, say f >= 0, by constraint
615 * f <= -1, add the inequalities of "j" that are valid for "i"
616 * and check if the result is a subset of basic map "j".
617 * To improve the chances of the subset relation being detected,
618 * any variable that only attains a single integer value
619 * in the tableau of "i" is first fixed to that value.
620 * If the result is a subset, then we know that this result is exactly equal
621 * to basic map "j" since all its constraints are valid for basic map "j".
622 * By combining the valid constraints of "i" (all equalities and all
623 * inequalities except "k") and the valid constraints of "j" we therefore
624 * obtain a basic map that is equal to their union.
625 * In this case, there is no need to perform a rollback of the tableau
626 * since it is going to be destroyed in fuse().
629 * |\__ |\__
630 * | \__ | \__
631 * | \_ => | \__
632 * |_______| _ |_________\
635 * |\ |\
636 * | \ | \
637 * | \ | \
638 * | | | \
639 * | ||\ => | \
640 * | || \ | \
641 * | || | | |
642 * |__||_/ |_____/
644 static enum isl_change is_adj_ineq_extension(int i, int j,
645 struct isl_coalesce_info *info)
647 int k;
648 struct isl_tab_undo *snap;
649 unsigned n_eq = info[i].bmap->n_eq;
650 unsigned total = isl_basic_map_total_dim(info[i].bmap);
651 isl_stat r;
652 isl_bool super;
654 if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
655 return isl_change_error;
657 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
658 if (k < 0)
659 isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
660 "info[i].ineq should have exactly one STATUS_ADJ_INEQ",
661 return isl_change_error);
663 snap = isl_tab_snap(info[i].tab);
665 if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
666 return isl_change_error;
668 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
669 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
670 r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
671 isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
672 isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
673 if (r < 0)
674 return isl_change_error;
676 for (k = 0; k < info[j].bmap->n_ineq; ++k) {
677 if (info[j].ineq[k] != STATUS_VALID)
678 continue;
679 if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
680 return isl_change_error;
682 if (isl_tab_detect_constants(info[i].tab) < 0)
683 return isl_change_error;
685 super = contains(&info[j], info[i].tab);
686 if (super < 0)
687 return isl_change_error;
688 if (super)
689 return fuse(i, j, info, NULL, 0, 0);
691 if (isl_tab_rollback(info[i].tab, snap) < 0)
692 return isl_change_error;
694 return isl_change_none;
698 /* Both basic maps have at least one inequality with and adjacent
699 * (but opposite) inequality in the other basic map.
700 * Check that there are no cut constraints and that there is only
701 * a single pair of adjacent inequalities.
702 * If so, we can replace the pair by a single basic map described
703 * by all but the pair of adjacent inequalities.
704 * Any additional points introduced lie strictly between the two
705 * adjacent hyperplanes and can therefore be integral.
707 * ____ _____
708 * / ||\ / \
709 * / || \ / \
710 * \ || \ => \ \
711 * \ || / \ /
712 * \___||_/ \_____/
714 * The test for a single pair of adjancent inequalities is important
715 * for avoiding the combination of two basic maps like the following
717 * /|
718 * / |
719 * /__|
720 * _____
721 * | |
722 * | |
723 * |___|
725 * If there are some cut constraints on one side, then we may
726 * still be able to fuse the two basic maps, but we need to perform
727 * some additional checks in is_adj_ineq_extension.
729 static enum isl_change check_adj_ineq(int i, int j,
730 struct isl_coalesce_info *info)
732 int count_i, count_j;
733 int cut_i, cut_j;
735 count_i = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
736 count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
738 if (count_i != 1 && count_j != 1)
739 return isl_change_none;
741 cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
742 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
743 cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
744 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_CUT);
746 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
747 return fuse(i, j, info, NULL, 0, 0);
749 if (count_i == 1 && !cut_i)
750 return is_adj_ineq_extension(i, j, info);
752 if (count_j == 1 && !cut_j)
753 return is_adj_ineq_extension(j, i, info);
755 return isl_change_none;
758 /* Given an affine transformation matrix "T", does row "row" represent
759 * anything other than a unit vector (possibly shifted by a constant)
760 * that is not involved in any of the other rows?
762 * That is, if a constraint involves the variable corresponding to
763 * the row, then could its preimage by "T" have any coefficients
764 * that are different from those in the original constraint?
766 static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
768 int i, j;
769 int len = T->n_col - 1;
771 i = isl_seq_first_non_zero(T->row[row] + 1, len);
772 if (i < 0)
773 return 1;
774 if (!isl_int_is_one(T->row[row][1 + i]) &&
775 !isl_int_is_negone(T->row[row][1 + i]))
776 return 1;
778 j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
779 if (j >= 0)
780 return 1;
782 for (j = 1; j < T->n_row; ++j) {
783 if (j == row)
784 continue;
785 if (!isl_int_is_zero(T->row[j][1 + i]))
786 return 1;
789 return 0;
792 /* Does inequality constraint "ineq" of "bmap" involve any of
793 * the variables marked in "affected"?
794 * "total" is the total number of variables, i.e., the number
795 * of entries in "affected".
797 static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
798 int *affected, int total)
800 int i;
802 for (i = 0; i < total; ++i) {
803 if (!affected[i])
804 continue;
805 if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
806 return isl_bool_true;
809 return isl_bool_false;
812 /* Given the compressed version of inequality constraint "ineq"
813 * of info->bmap in "v", check if the constraint can be tightened,
814 * where the compression is based on an equality constraint valid
815 * for info->tab.
816 * If so, add the tightened version of the inequality constraint
817 * to info->tab. "v" may be modified by this function.
819 * That is, if the compressed constraint is of the form
821 * m f() + c >= 0
823 * with 0 < c < m, then it is equivalent to
825 * f() >= 0
827 * This means that c can also be subtracted from the original,
828 * uncompressed constraint without affecting the integer points
829 * in info->tab. Add this tightened constraint as an extra row
830 * to info->tab to make this information explicitly available.
832 static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
833 int ineq, __isl_take isl_vec *v)
835 isl_ctx *ctx;
836 isl_stat r;
838 if (!v)
839 return NULL;
841 ctx = isl_vec_get_ctx(v);
842 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
843 if (isl_int_is_zero(ctx->normalize_gcd) ||
844 isl_int_is_one(ctx->normalize_gcd)) {
845 return v;
848 v = isl_vec_cow(v);
849 if (!v)
850 return NULL;
852 isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
853 if (isl_int_is_zero(v->el[0]))
854 return v;
856 if (isl_tab_extend_cons(info->tab, 1) < 0)
857 return isl_vec_free(v);
859 isl_int_sub(info->bmap->ineq[ineq][0],
860 info->bmap->ineq[ineq][0], v->el[0]);
861 r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
862 isl_int_add(info->bmap->ineq[ineq][0],
863 info->bmap->ineq[ineq][0], v->el[0]);
865 if (r < 0)
866 return isl_vec_free(v);
868 return v;
871 /* Tighten the (non-redundant) constraints on the facet represented
872 * by info->tab.
873 * In particular, on input, info->tab represents the result
874 * of relaxing the "n" inequality constraints of info->bmap in "relaxed"
875 * by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
876 * replacing the one at index "l" by the corresponding equality,
877 * i.e., f_k + 1 = 0, with k = relaxed[l].
879 * Compute a variable compression from the equality constraint f_k + 1 = 0
880 * and use it to tighten the other constraints of info->bmap
881 * (that is, all constraints that have not been relaxed),
882 * updating info->tab (and leaving info->bmap untouched).
883 * The compression handles essentially two cases, one where a variable
884 * is assigned a fixed value and can therefore be eliminated, and one
885 * where one variable is a shifted multiple of some other variable and
886 * can therefore be replaced by that multiple.
887 * Gaussian elimination would also work for the first case, but for
888 * the second case, the effectiveness would depend on the order
889 * of the variables.
890 * After compression, some of the constraints may have coefficients
891 * with a common divisor. If this divisor does not divide the constant
892 * term, then the constraint can be tightened.
893 * The tightening is performed on the tableau info->tab by introducing
894 * extra (temporary) constraints.
896 * Only constraints that are possibly affected by the compression are
897 * considered. In particular, if the constraint only involves variables
898 * that are directly mapped to a distinct set of other variables, then
899 * no common divisor can be introduced and no tightening can occur.
901 * It is important to only consider the non-redundant constraints
902 * since the facet constraint has been relaxed prior to the call
903 * to this function, meaning that the constraints that were redundant
904 * prior to the relaxation may no longer be redundant.
905 * These constraints will be ignored in the fused result, so
906 * the fusion detection should not exploit them.
908 static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
909 int n, int *relaxed, int l)
911 unsigned total;
912 isl_ctx *ctx;
913 isl_vec *v = NULL;
914 isl_mat *T;
915 int i;
916 int k;
917 int *affected;
919 k = relaxed[l];
920 ctx = isl_basic_map_get_ctx(info->bmap);
921 total = isl_basic_map_total_dim(info->bmap);
922 isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
923 T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
924 T = isl_mat_variable_compression(T, NULL);
925 isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
926 if (!T)
927 return isl_stat_error;
928 if (T->n_col == 0) {
929 isl_mat_free(T);
930 return isl_stat_ok;
933 affected = isl_alloc_array(ctx, int, total);
934 if (!affected)
935 goto error;
937 for (i = 0; i < total; ++i)
938 affected[i] = not_unique_unit_row(T, 1 + i);
940 for (i = 0; i < info->bmap->n_ineq; ++i) {
941 isl_bool handle;
942 if (any(relaxed, n, i))
943 continue;
944 if (info->ineq[i] == STATUS_REDUNDANT)
945 continue;
946 handle = is_affected(info->bmap, i, affected, total);
947 if (handle < 0)
948 goto error;
949 if (!handle)
950 continue;
951 v = isl_vec_alloc(ctx, 1 + total);
952 if (!v)
953 goto error;
954 isl_seq_cpy(v->el, info->bmap->ineq[i], 1 + total);
955 v = isl_vec_mat_product(v, isl_mat_copy(T));
956 v = try_tightening(info, i, v);
957 isl_vec_free(v);
958 if (!v)
959 goto error;
962 isl_mat_free(T);
963 free(affected);
964 return isl_stat_ok;
965 error:
966 isl_mat_free(T);
967 free(affected);
968 return isl_stat_error;
971 /* Replace the basic maps "i" and "j" by an extension of "i"
972 * along the "n" inequality constraints in "relax" by one.
973 * The tableau info[i].tab has already been extended.
974 * Extend info[i].bmap accordingly by relaxing all constraints in "relax"
975 * by one.
976 * Each integer division that does not have exactly the same
977 * definition in "i" and "j" is marked unknown and the basic map
978 * is scheduled to be simplified in an attempt to recover
979 * the integer division definition.
980 * Place the extension in the position that is the smallest of i and j.
982 static enum isl_change extend(int i, int j, int n, int *relax,
983 struct isl_coalesce_info *info)
985 int l;
986 unsigned total;
988 info[i].bmap = isl_basic_map_cow(info[i].bmap);
989 if (!info[i].bmap)
990 return isl_change_error;
991 total = isl_basic_map_total_dim(info[i].bmap);
992 for (l = 0; l < info[i].bmap->n_div; ++l)
993 if (!isl_seq_eq(info[i].bmap->div[l],
994 info[j].bmap->div[l], 1 + 1 + total)) {
995 isl_int_set_si(info[i].bmap->div[l][0], 0);
996 info[i].simplify = 1;
998 for (l = 0; l < n; ++l)
999 isl_int_add_ui(info[i].bmap->ineq[relax[l]][0],
1000 info[i].bmap->ineq[relax[l]][0], 1);
1001 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
1002 drop(&info[j]);
1003 if (j < i)
1004 exchange(&info[i], &info[j]);
1005 return isl_change_fuse;
1008 /* Basic map "i" has "n" inequality constraints (collected in "relax")
1009 * that are such that they include basic map "j" if they are relaxed
1010 * by one. All the other inequalities are valid for "j".
1011 * Check if basic map "j" forms an extension of basic map "i".
1013 * In particular, relax the constraints in "relax", compute the corresponding
1014 * facets one by one and check whether each of these is included
1015 * in the other basic map.
1016 * Before testing for inclusion, the constraints on each facet
1017 * are tightened to increase the chance of an inclusion being detected.
1018 * (Adding the valid constraints of "j" to the tableau of "i", as is done
1019 * in is_adj_ineq_extension, may further increase those chances, but this
1020 * is not currently done.)
1021 * If each facet is included, we know that relaxing the constraints extends
1022 * the basic map with exactly the other basic map (we already know that this
1023 * other basic map is included in the extension, because all other
1024 * inequality constraints are valid of "j") and we can replace the
1025 * two basic maps by this extension.
1027 * If any of the relaxed constraints turn out to be redundant, then bail out.
1028 * isl_tab_select_facet refuses to handle such constraints. It may be
1029 * possible to handle them anyway by making a distinction between
1030 * redundant constraints with a corresponding facet that still intersects
1031 * the set (allowing isl_tab_select_facet to handle them) and
1032 * those where the facet does not intersect the set (which can be ignored
1033 * because the empty facet is trivially included in the other disjunct).
1034 * However, relaxed constraints that turn out to be redundant should
1035 * be fairly rare and no such instance has been reported where
1036 * coalescing would be successful.
1037 * ____ _____
1038 * / || / |
1039 * / || / |
1040 * \ || => \ |
1041 * \ || \ |
1042 * \___|| \____|
1045 * \ |\
1046 * |\\ | \
1047 * | \\ | \
1048 * | | => | /
1049 * | / | /
1050 * |/ |/
1052 static enum isl_change is_relaxed_extension(int i, int j, int n, int *relax,
1053 struct isl_coalesce_info *info)
1055 int l;
1056 isl_bool super;
1057 struct isl_tab_undo *snap, *snap2;
1058 unsigned n_eq = info[i].bmap->n_eq;
1060 for (l = 0; l < n; ++l)
1061 if (isl_tab_is_equality(info[i].tab, n_eq + relax[l]))
1062 return isl_change_none;
1064 snap = isl_tab_snap(info[i].tab);
1065 for (l = 0; l < n; ++l)
1066 if (isl_tab_relax(info[i].tab, n_eq + relax[l]) < 0)
1067 return isl_change_error;
1068 for (l = 0; l < n; ++l) {
1069 if (!isl_tab_is_redundant(info[i].tab, n_eq + relax[l]))
1070 continue;
1071 if (isl_tab_rollback(info[i].tab, snap) < 0)
1072 return isl_change_error;
1073 return isl_change_none;
1075 snap2 = isl_tab_snap(info[i].tab);
1076 for (l = 0; l < n; ++l) {
1077 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1078 return isl_change_error;
1079 if (isl_tab_select_facet(info[i].tab, n_eq + relax[l]) < 0)
1080 return isl_change_error;
1081 if (tighten_on_relaxed_facet(&info[i], n, relax, l) < 0)
1082 return isl_change_error;
1083 super = contains(&info[j], info[i].tab);
1084 if (super < 0)
1085 return isl_change_error;
1086 if (super)
1087 continue;
1088 if (isl_tab_rollback(info[i].tab, snap) < 0)
1089 return isl_change_error;
1090 return isl_change_none;
1093 if (isl_tab_rollback(info[i].tab, snap2) < 0)
1094 return isl_change_error;
1095 return extend(i, j, n, relax, info);
1098 /* Data structure that keeps track of the wrapping constraints
1099 * and of information to bound the coefficients of those constraints.
1101 * bound is set if we want to apply a bound on the coefficients
1102 * mat contains the wrapping constraints
1103 * max is the bound on the coefficients (if bound is set)
1105 struct isl_wraps {
1106 int bound;
1107 isl_mat *mat;
1108 isl_int max;
1111 /* Update wraps->max to be greater than or equal to the coefficients
1112 * in the equalities and inequalities of info->bmap that can be removed
1113 * if we end up applying wrapping.
1115 static isl_stat wraps_update_max(struct isl_wraps *wraps,
1116 struct isl_coalesce_info *info)
1118 int k;
1119 isl_int max_k;
1120 unsigned total = isl_basic_map_total_dim(info->bmap);
1122 isl_int_init(max_k);
1124 for (k = 0; k < info->bmap->n_eq; ++k) {
1125 if (info->eq[2 * k] == STATUS_VALID &&
1126 info->eq[2 * k + 1] == STATUS_VALID)
1127 continue;
1128 isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
1129 if (isl_int_abs_gt(max_k, wraps->max))
1130 isl_int_set(wraps->max, max_k);
1133 for (k = 0; k < info->bmap->n_ineq; ++k) {
1134 if (info->ineq[k] == STATUS_VALID ||
1135 info->ineq[k] == STATUS_REDUNDANT)
1136 continue;
1137 isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
1138 if (isl_int_abs_gt(max_k, wraps->max))
1139 isl_int_set(wraps->max, max_k);
1142 isl_int_clear(max_k);
1144 return isl_stat_ok;
1147 /* Initialize the isl_wraps data structure.
1148 * If we want to bound the coefficients of the wrapping constraints,
1149 * we set wraps->max to the largest coefficient
1150 * in the equalities and inequalities that can be removed if we end up
1151 * applying wrapping.
1153 static isl_stat wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
1154 struct isl_coalesce_info *info, int i, int j)
1156 isl_ctx *ctx;
1158 wraps->bound = 0;
1159 wraps->mat = mat;
1160 if (!mat)
1161 return isl_stat_error;
1162 ctx = isl_mat_get_ctx(mat);
1163 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
1164 if (!wraps->bound)
1165 return isl_stat_ok;
1166 isl_int_init(wraps->max);
1167 isl_int_set_si(wraps->max, 0);
1168 if (wraps_update_max(wraps, &info[i]) < 0)
1169 return isl_stat_error;
1170 if (wraps_update_max(wraps, &info[j]) < 0)
1171 return isl_stat_error;
1173 return isl_stat_ok;
1176 /* Free the contents of the isl_wraps data structure.
1178 static void wraps_free(struct isl_wraps *wraps)
1180 isl_mat_free(wraps->mat);
1181 if (wraps->bound)
1182 isl_int_clear(wraps->max);
1185 /* Is the wrapping constraint in row "row" allowed?
1187 * If wraps->bound is set, we check that none of the coefficients
1188 * is greater than wraps->max.
1190 static int allow_wrap(struct isl_wraps *wraps, int row)
1192 int i;
1194 if (!wraps->bound)
1195 return 1;
1197 for (i = 1; i < wraps->mat->n_col; ++i)
1198 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
1199 return 0;
1201 return 1;
1204 /* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
1205 * to include "set" and add the result in position "w" of "wraps".
1206 * "len" is the total number of coefficients in "bound" and "ineq".
1207 * Return 1 on success, 0 on failure and -1 on error.
1208 * Wrapping can fail if the result of wrapping is equal to "bound"
1209 * or if we want to bound the sizes of the coefficients and
1210 * the wrapped constraint does not satisfy this bound.
1212 static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
1213 isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
1215 isl_seq_cpy(wraps->mat->row[w], bound, len);
1216 if (negate) {
1217 isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
1218 ineq = wraps->mat->row[w + 1];
1220 if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
1221 return -1;
1222 if (isl_seq_eq(wraps->mat->row[w], bound, len))
1223 return 0;
1224 if (!allow_wrap(wraps, w))
1225 return 0;
1226 return 1;
1229 /* For each constraint in info->bmap that is not redundant (as determined
1230 * by info->tab) and that is not a valid constraint for the other basic map,
1231 * wrap the constraint around "bound" such that it includes the whole
1232 * set "set" and append the resulting constraint to "wraps".
1233 * Note that the constraints that are valid for the other basic map
1234 * will be added to the combined basic map by default, so there is
1235 * no need to wrap them.
1236 * The caller wrap_in_facets even relies on this function not wrapping
1237 * any constraints that are already valid.
1238 * "wraps" is assumed to have been pre-allocated to the appropriate size.
1239 * wraps->n_row is the number of actual wrapped constraints that have
1240 * been added.
1241 * If any of the wrapping problems results in a constraint that is
1242 * identical to "bound", then this means that "set" is unbounded in such
1243 * way that no wrapping is possible. If this happens then wraps->n_row
1244 * is reset to zero.
1245 * Similarly, if we want to bound the coefficients of the wrapping
1246 * constraints and a newly added wrapping constraint does not
1247 * satisfy the bound, then wraps->n_row is also reset to zero.
1249 static isl_stat add_wraps(struct isl_wraps *wraps,
1250 struct isl_coalesce_info *info, isl_int *bound, __isl_keep isl_set *set)
1252 int l, m;
1253 int w;
1254 int added;
1255 isl_basic_map *bmap = info->bmap;
1256 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1258 w = wraps->mat->n_row;
1260 for (l = 0; l < bmap->n_ineq; ++l) {
1261 if (info->ineq[l] == STATUS_VALID ||
1262 info->ineq[l] == STATUS_REDUNDANT)
1263 continue;
1264 if (isl_seq_is_neg(bound, bmap->ineq[l], len))
1265 continue;
1266 if (isl_seq_eq(bound, bmap->ineq[l], len))
1267 continue;
1268 if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
1269 continue;
1271 added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
1272 if (added < 0)
1273 return isl_stat_error;
1274 if (!added)
1275 goto unbounded;
1276 ++w;
1278 for (l = 0; l < bmap->n_eq; ++l) {
1279 if (isl_seq_is_neg(bound, bmap->eq[l], len))
1280 continue;
1281 if (isl_seq_eq(bound, bmap->eq[l], len))
1282 continue;
1284 for (m = 0; m < 2; ++m) {
1285 if (info->eq[2 * l + m] == STATUS_VALID)
1286 continue;
1287 added = add_wrap(wraps, w, bound, bmap->eq[l], len,
1288 set, !m);
1289 if (added < 0)
1290 return isl_stat_error;
1291 if (!added)
1292 goto unbounded;
1293 ++w;
1297 wraps->mat->n_row = w;
1298 return isl_stat_ok;
1299 unbounded:
1300 wraps->mat->n_row = 0;
1301 return isl_stat_ok;
1304 /* Check if the constraints in "wraps" from "first" until the last
1305 * are all valid for the basic set represented by "tab".
1306 * If not, wraps->n_row is set to zero.
1308 static int check_wraps(__isl_keep isl_mat *wraps, int first,
1309 struct isl_tab *tab)
1311 int i;
1313 for (i = first; i < wraps->n_row; ++i) {
1314 enum isl_ineq_type type;
1315 type = isl_tab_ineq_type(tab, wraps->row[i]);
1316 if (type == isl_ineq_error)
1317 return -1;
1318 if (type == isl_ineq_redundant)
1319 continue;
1320 wraps->n_row = 0;
1321 return 0;
1324 return 0;
1327 /* Return a set that corresponds to the non-redundant constraints
1328 * (as recorded in tab) of bmap.
1330 * It's important to remove the redundant constraints as some
1331 * of the other constraints may have been modified after the
1332 * constraints were marked redundant.
1333 * In particular, a constraint may have been relaxed.
1334 * Redundant constraints are ignored when a constraint is relaxed
1335 * and should therefore continue to be ignored ever after.
1336 * Otherwise, the relaxation might be thwarted by some of
1337 * these constraints.
1339 * Update the underlying set to ensure that the dimension doesn't change.
1340 * Otherwise the integer divisions could get dropped if the tab
1341 * turns out to be empty.
1343 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
1344 struct isl_tab *tab)
1346 isl_basic_set *bset;
1348 bmap = isl_basic_map_copy(bmap);
1349 bset = isl_basic_map_underlying_set(bmap);
1350 bset = isl_basic_set_cow(bset);
1351 bset = isl_basic_set_update_from_tab(bset, tab);
1352 return isl_set_from_basic_set(bset);
1355 /* Wrap the constraints of info->bmap that bound the facet defined
1356 * by inequality "k" around (the opposite of) this inequality to
1357 * include "set". "bound" may be used to store the negated inequality.
1358 * Since the wrapped constraints are not guaranteed to contain the whole
1359 * of info->bmap, we check them in check_wraps.
1360 * If any of the wrapped constraints turn out to be invalid, then
1361 * check_wraps will reset wrap->n_row to zero.
1363 static isl_stat add_wraps_around_facet(struct isl_wraps *wraps,
1364 struct isl_coalesce_info *info, int k, isl_int *bound,
1365 __isl_keep isl_set *set)
1367 struct isl_tab_undo *snap;
1368 int n;
1369 unsigned total = isl_basic_map_total_dim(info->bmap);
1371 snap = isl_tab_snap(info->tab);
1373 if (isl_tab_select_facet(info->tab, info->bmap->n_eq + k) < 0)
1374 return isl_stat_error;
1375 if (isl_tab_detect_redundant(info->tab) < 0)
1376 return isl_stat_error;
1378 isl_seq_neg(bound, info->bmap->ineq[k], 1 + total);
1380 n = wraps->mat->n_row;
1381 if (add_wraps(wraps, info, bound, set) < 0)
1382 return isl_stat_error;
1384 if (isl_tab_rollback(info->tab, snap) < 0)
1385 return isl_stat_error;
1386 if (check_wraps(wraps->mat, n, info->tab) < 0)
1387 return isl_stat_error;
1389 return isl_stat_ok;
1392 /* Given a basic set i with a constraint k that is adjacent to
1393 * basic set j, check if we can wrap
1394 * both the facet corresponding to k (if "wrap_facet" is set) and basic map j
1395 * (always) around their ridges to include the other set.
1396 * If so, replace the pair of basic sets by their union.
1398 * All constraints of i (except k) are assumed to be valid or
1399 * cut constraints for j.
1400 * Wrapping the cut constraints to include basic map j may result
1401 * in constraints that are no longer valid of basic map i
1402 * we have to check that the resulting wrapping constraints are valid for i.
1403 * If "wrap_facet" is not set, then all constraints of i (except k)
1404 * are assumed to be valid for j.
1405 * ____ _____
1406 * / | / \
1407 * / || / |
1408 * \ || => \ |
1409 * \ || \ |
1410 * \___|| \____|
1413 static enum isl_change can_wrap_in_facet(int i, int j, int k,
1414 struct isl_coalesce_info *info, int wrap_facet)
1416 enum isl_change change = isl_change_none;
1417 struct isl_wraps wraps;
1418 isl_ctx *ctx;
1419 isl_mat *mat;
1420 struct isl_set *set_i = NULL;
1421 struct isl_set *set_j = NULL;
1422 struct isl_vec *bound = NULL;
1423 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1425 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1426 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1427 ctx = isl_basic_map_get_ctx(info[i].bmap);
1428 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1429 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1430 1 + total);
1431 if (wraps_init(&wraps, mat, info, i, j) < 0)
1432 goto error;
1433 bound = isl_vec_alloc(ctx, 1 + total);
1434 if (!set_i || !set_j || !bound)
1435 goto error;
1437 isl_seq_cpy(bound->el, info[i].bmap->ineq[k], 1 + total);
1438 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1440 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1441 wraps.mat->n_row = 1;
1443 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1444 goto error;
1445 if (!wraps.mat->n_row)
1446 goto unbounded;
1448 if (wrap_facet) {
1449 if (add_wraps_around_facet(&wraps, &info[i], k,
1450 bound->el, set_j) < 0)
1451 goto error;
1452 if (!wraps.mat->n_row)
1453 goto unbounded;
1456 change = fuse(i, j, info, wraps.mat, 0, 0);
1458 unbounded:
1459 wraps_free(&wraps);
1461 isl_set_free(set_i);
1462 isl_set_free(set_j);
1464 isl_vec_free(bound);
1466 return change;
1467 error:
1468 wraps_free(&wraps);
1469 isl_vec_free(bound);
1470 isl_set_free(set_i);
1471 isl_set_free(set_j);
1472 return isl_change_error;
1475 /* Given a cut constraint t(x) >= 0 of basic map i, stored in row "w"
1476 * of wrap.mat, replace it by its relaxed version t(x) + 1 >= 0, and
1477 * add wrapping constraints to wrap.mat for all constraints
1478 * of basic map j that bound the part of basic map j that sticks out
1479 * of the cut constraint.
1480 * "set_i" is the underlying set of basic map i.
1481 * If any wrapping fails, then wraps->mat.n_row is reset to zero.
1483 * In particular, we first intersect basic map j with t(x) + 1 = 0.
1484 * If the result is empty, then t(x) >= 0 was actually a valid constraint
1485 * (with respect to the integer points), so we add t(x) >= 0 instead.
1486 * Otherwise, we wrap the constraints of basic map j that are not
1487 * redundant in this intersection and that are not already valid
1488 * for basic map i over basic map i.
1489 * Note that it is sufficient to wrap the constraints to include
1490 * basic map i, because we will only wrap the constraints that do
1491 * not include basic map i already. The wrapped constraint will
1492 * therefore be more relaxed compared to the original constraint.
1493 * Since the original constraint is valid for basic map j, so is
1494 * the wrapped constraint.
1496 static isl_stat wrap_in_facet(struct isl_wraps *wraps, int w,
1497 struct isl_coalesce_info *info_j, __isl_keep isl_set *set_i,
1498 struct isl_tab_undo *snap)
1500 isl_int_add_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1501 if (isl_tab_add_eq(info_j->tab, wraps->mat->row[w]) < 0)
1502 return isl_stat_error;
1503 if (isl_tab_detect_redundant(info_j->tab) < 0)
1504 return isl_stat_error;
1506 if (info_j->tab->empty)
1507 isl_int_sub_ui(wraps->mat->row[w][0], wraps->mat->row[w][0], 1);
1508 else if (add_wraps(wraps, info_j, wraps->mat->row[w], set_i) < 0)
1509 return isl_stat_error;
1511 if (isl_tab_rollback(info_j->tab, snap) < 0)
1512 return isl_stat_error;
1514 return isl_stat_ok;
1517 /* Given a pair of basic maps i and j such that j sticks out
1518 * of i at n cut constraints, each time by at most one,
1519 * try to compute wrapping constraints and replace the two
1520 * basic maps by a single basic map.
1521 * The other constraints of i are assumed to be valid for j.
1522 * "set_i" is the underlying set of basic map i.
1523 * "wraps" has been initialized to be of the right size.
1525 * For each cut constraint t(x) >= 0 of i, we add the relaxed version
1526 * t(x) + 1 >= 0, along with wrapping constraints for all constraints
1527 * of basic map j that bound the part of basic map j that sticks out
1528 * of the cut constraint.
1530 * If any wrapping fails, i.e., if we cannot wrap to touch
1531 * the union, then we give up.
1532 * Otherwise, the pair of basic maps is replaced by their union.
1534 static enum isl_change try_wrap_in_facets(int i, int j,
1535 struct isl_coalesce_info *info, struct isl_wraps *wraps,
1536 __isl_keep isl_set *set_i)
1538 int k, l, w;
1539 unsigned total;
1540 struct isl_tab_undo *snap;
1542 total = isl_basic_map_total_dim(info[i].bmap);
1544 snap = isl_tab_snap(info[j].tab);
1546 wraps->mat->n_row = 0;
1548 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1549 for (l = 0; l < 2; ++l) {
1550 if (info[i].eq[2 * k + l] != STATUS_CUT)
1551 continue;
1552 w = wraps->mat->n_row++;
1553 if (l == 0)
1554 isl_seq_neg(wraps->mat->row[w],
1555 info[i].bmap->eq[k], 1 + total);
1556 else
1557 isl_seq_cpy(wraps->mat->row[w],
1558 info[i].bmap->eq[k], 1 + total);
1559 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1560 return isl_change_error;
1562 if (!wraps->mat->n_row)
1563 return isl_change_none;
1567 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1568 if (info[i].ineq[k] != STATUS_CUT)
1569 continue;
1570 w = wraps->mat->n_row++;
1571 isl_seq_cpy(wraps->mat->row[w],
1572 info[i].bmap->ineq[k], 1 + total);
1573 if (wrap_in_facet(wraps, w, &info[j], set_i, snap) < 0)
1574 return isl_change_error;
1576 if (!wraps->mat->n_row)
1577 return isl_change_none;
1580 return fuse(i, j, info, wraps->mat, 0, 1);
1583 /* Given a pair of basic maps i and j such that j sticks out
1584 * of i at n cut constraints, each time by at most one,
1585 * try to compute wrapping constraints and replace the two
1586 * basic maps by a single basic map.
1587 * The other constraints of i are assumed to be valid for j.
1589 * The core computation is performed by try_wrap_in_facets.
1590 * This function simply extracts an underlying set representation
1591 * of basic map i and initializes the data structure for keeping
1592 * track of wrapping constraints.
1594 static enum isl_change wrap_in_facets(int i, int j, int n,
1595 struct isl_coalesce_info *info)
1597 enum isl_change change = isl_change_none;
1598 struct isl_wraps wraps;
1599 isl_ctx *ctx;
1600 isl_mat *mat;
1601 isl_set *set_i = NULL;
1602 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1603 int max_wrap;
1605 if (isl_tab_extend_cons(info[j].tab, 1) < 0)
1606 return isl_change_error;
1608 max_wrap = 1 + 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
1609 max_wrap *= n;
1611 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1612 ctx = isl_basic_map_get_ctx(info[i].bmap);
1613 mat = isl_mat_alloc(ctx, max_wrap, 1 + total);
1614 if (wraps_init(&wraps, mat, info, i, j) < 0)
1615 goto error;
1616 if (!set_i)
1617 goto error;
1619 change = try_wrap_in_facets(i, j, info, &wraps, set_i);
1621 wraps_free(&wraps);
1622 isl_set_free(set_i);
1624 return change;
1625 error:
1626 wraps_free(&wraps);
1627 isl_set_free(set_i);
1628 return isl_change_error;
1631 /* Return the effect of inequality "ineq" on the tableau "tab",
1632 * after relaxing the constant term of "ineq" by one.
1634 static enum isl_ineq_type type_of_relaxed(struct isl_tab *tab, isl_int *ineq)
1636 enum isl_ineq_type type;
1638 isl_int_add_ui(ineq[0], ineq[0], 1);
1639 type = isl_tab_ineq_type(tab, ineq);
1640 isl_int_sub_ui(ineq[0], ineq[0], 1);
1642 return type;
1645 /* Given two basic sets i and j,
1646 * check if relaxing all the cut constraints of i by one turns
1647 * them into valid constraint for j and check if we can wrap in
1648 * the bits that are sticking out.
1649 * If so, replace the pair by their union.
1651 * We first check if all relaxed cut inequalities of i are valid for j
1652 * and then try to wrap in the intersections of the relaxed cut inequalities
1653 * with j.
1655 * During this wrapping, we consider the points of j that lie at a distance
1656 * of exactly 1 from i. In particular, we ignore the points that lie in
1657 * between this lower-dimensional space and the basic map i.
1658 * We can therefore only apply this to integer maps.
1659 * ____ _____
1660 * / ___|_ / \
1661 * / | | / |
1662 * \ | | => \ |
1663 * \|____| \ |
1664 * \___| \____/
1666 * _____ ______
1667 * | ____|_ | \
1668 * | | | | |
1669 * | | | => | |
1670 * |_| | | |
1671 * |_____| \______|
1673 * _______
1674 * | |
1675 * | |\ |
1676 * | | \ |
1677 * | | \ |
1678 * | | \|
1679 * | | \
1680 * | |_____\
1681 * | |
1682 * |_______|
1684 * Wrapping can fail if the result of wrapping one of the facets
1685 * around its edges does not produce any new facet constraint.
1686 * In particular, this happens when we try to wrap in unbounded sets.
1688 * _______________________________________________________________________
1690 * | ___
1691 * | | |
1692 * |_| |_________________________________________________________________
1693 * |___|
1695 * The following is not an acceptable result of coalescing the above two
1696 * sets as it includes extra integer points.
1697 * _______________________________________________________________________
1699 * |
1700 * |
1702 * \______________________________________________________________________
1704 static enum isl_change can_wrap_in_set(int i, int j,
1705 struct isl_coalesce_info *info)
1707 int k, l;
1708 int n;
1709 unsigned total;
1711 if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) ||
1712 ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
1713 return isl_change_none;
1715 n = count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT);
1716 n += count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1717 if (n == 0)
1718 return isl_change_none;
1720 total = isl_basic_map_total_dim(info[i].bmap);
1721 for (k = 0; k < info[i].bmap->n_eq; ++k) {
1722 for (l = 0; l < 2; ++l) {
1723 enum isl_ineq_type type;
1725 if (info[i].eq[2 * k + l] != STATUS_CUT)
1726 continue;
1728 if (l == 0)
1729 isl_seq_neg(info[i].bmap->eq[k],
1730 info[i].bmap->eq[k], 1 + total);
1731 type = type_of_relaxed(info[j].tab,
1732 info[i].bmap->eq[k]);
1733 if (l == 0)
1734 isl_seq_neg(info[i].bmap->eq[k],
1735 info[i].bmap->eq[k], 1 + total);
1736 if (type == isl_ineq_error)
1737 return isl_change_error;
1738 if (type != isl_ineq_redundant)
1739 return isl_change_none;
1743 for (k = 0; k < info[i].bmap->n_ineq; ++k) {
1744 enum isl_ineq_type type;
1746 if (info[i].ineq[k] != STATUS_CUT)
1747 continue;
1749 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[k]);
1750 if (type == isl_ineq_error)
1751 return isl_change_error;
1752 if (type != isl_ineq_redundant)
1753 return isl_change_none;
1756 return wrap_in_facets(i, j, n, info);
1759 /* Check if either i or j has only cut constraints that can
1760 * be used to wrap in (a facet of) the other basic set.
1761 * if so, replace the pair by their union.
1763 static enum isl_change check_wrap(int i, int j, struct isl_coalesce_info *info)
1765 enum isl_change change = isl_change_none;
1767 change = can_wrap_in_set(i, j, info);
1768 if (change != isl_change_none)
1769 return change;
1771 change = can_wrap_in_set(j, i, info);
1772 return change;
1775 /* Check if all inequality constraints of "i" that cut "j" cease
1776 * to be cut constraints if they are relaxed by one.
1777 * If so, collect the cut constraints in "list".
1778 * The caller is responsible for allocating "list".
1780 static isl_bool all_cut_by_one(int i, int j, struct isl_coalesce_info *info,
1781 int *list)
1783 int l, n;
1785 n = 0;
1786 for (l = 0; l < info[i].bmap->n_ineq; ++l) {
1787 enum isl_ineq_type type;
1789 if (info[i].ineq[l] != STATUS_CUT)
1790 continue;
1791 type = type_of_relaxed(info[j].tab, info[i].bmap->ineq[l]);
1792 if (type == isl_ineq_error)
1793 return isl_bool_error;
1794 if (type != isl_ineq_redundant)
1795 return isl_bool_false;
1796 list[n++] = l;
1799 return isl_bool_true;
1802 /* Given two basic maps such that "j" has at least one equality constraint
1803 * that is adjacent to an inequality constraint of "i" and such that "i" has
1804 * exactly one inequality constraint that is adjacent to an equality
1805 * constraint of "j", check whether "i" can be extended to include "j" or
1806 * whether "j" can be wrapped into "i".
1807 * All remaining constraints of "i" and "j" are assumed to be valid
1808 * or cut constraints of the other basic map.
1809 * However, none of the equality constraints of "i" are cut constraints.
1811 * If "i" has any "cut" inequality constraints, then check if relaxing
1812 * each of them by one is sufficient for them to become valid.
1813 * If so, check if the inequality constraint adjacent to an equality
1814 * constraint of "j" along with all these cut constraints
1815 * can be relaxed by one to contain exactly "j".
1816 * Otherwise, or if this fails, check if "j" can be wrapped into "i".
1818 static enum isl_change check_single_adj_eq(int i, int j,
1819 struct isl_coalesce_info *info)
1821 enum isl_change change = isl_change_none;
1822 int k;
1823 int n_cut;
1824 int *relax;
1825 isl_ctx *ctx;
1826 isl_bool try_relax;
1828 n_cut = count(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
1830 k = find(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ);
1832 if (n_cut > 0) {
1833 ctx = isl_basic_map_get_ctx(info[i].bmap);
1834 relax = isl_calloc_array(ctx, int, 1 + n_cut);
1835 if (!relax)
1836 return isl_change_error;
1837 relax[0] = k;
1838 try_relax = all_cut_by_one(i, j, info, relax + 1);
1839 if (try_relax < 0)
1840 change = isl_change_error;
1841 } else {
1842 try_relax = isl_bool_true;
1843 relax = &k;
1845 if (try_relax && change == isl_change_none)
1846 change = is_relaxed_extension(i, j, 1 + n_cut, relax, info);
1847 if (n_cut > 0)
1848 free(relax);
1849 if (change != isl_change_none)
1850 return change;
1852 change = can_wrap_in_facet(i, j, k, info, n_cut > 0);
1854 return change;
1857 /* At least one of the basic maps has an equality that is adjacent
1858 * to inequality. Make sure that only one of the basic maps has
1859 * such an equality and that the other basic map has exactly one
1860 * inequality adjacent to an equality.
1861 * If the other basic map does not have such an inequality, then
1862 * check if all its constraints are either valid or cut constraints
1863 * and, if so, try wrapping in the first map into the second.
1864 * Otherwise, try to extend one basic map with the other or
1865 * wrap one basic map in the other.
1867 static enum isl_change check_adj_eq(int i, int j,
1868 struct isl_coalesce_info *info)
1870 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) &&
1871 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ))
1872 /* ADJ EQ TOO MANY */
1873 return isl_change_none;
1875 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ))
1876 return check_adj_eq(j, i, info);
1878 /* j has an equality adjacent to an inequality in i */
1880 if (count(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) != 1) {
1881 if (all_valid_or_cut(&info[i]))
1882 return can_wrap_in_set(i, j, info);
1883 return isl_change_none;
1885 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT))
1886 return isl_change_none;
1887 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ) ||
1888 any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
1889 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ))
1890 /* ADJ EQ TOO MANY */
1891 return isl_change_none;
1893 return check_single_adj_eq(i, j, info);
1896 /* The two basic maps lie on adjacent hyperplanes. In particular,
1897 * basic map "i" has an equality that lies parallel to basic map "j".
1898 * Check if we can wrap the facets around the parallel hyperplanes
1899 * to include the other set.
1901 * We perform basically the same operations as can_wrap_in_facet,
1902 * except that we don't need to select a facet of one of the sets.
1904 * \\ \\
1905 * \\ => \\
1906 * \ \|
1908 * If there is more than one equality of "i" adjacent to an equality of "j",
1909 * then the result will satisfy one or more equalities that are a linear
1910 * combination of these equalities. These will be encoded as pairs
1911 * of inequalities in the wrapping constraints and need to be made
1912 * explicit.
1914 static enum isl_change check_eq_adj_eq(int i, int j,
1915 struct isl_coalesce_info *info)
1917 int k;
1918 enum isl_change change = isl_change_none;
1919 int detect_equalities = 0;
1920 struct isl_wraps wraps;
1921 isl_ctx *ctx;
1922 isl_mat *mat;
1923 struct isl_set *set_i = NULL;
1924 struct isl_set *set_j = NULL;
1925 struct isl_vec *bound = NULL;
1926 unsigned total = isl_basic_map_total_dim(info[i].bmap);
1928 if (count(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ) != 1)
1929 detect_equalities = 1;
1931 k = find(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ);
1933 set_i = set_from_updated_bmap(info[i].bmap, info[i].tab);
1934 set_j = set_from_updated_bmap(info[j].bmap, info[j].tab);
1935 ctx = isl_basic_map_get_ctx(info[i].bmap);
1936 mat = isl_mat_alloc(ctx, 2 * (info[i].bmap->n_eq + info[j].bmap->n_eq) +
1937 info[i].bmap->n_ineq + info[j].bmap->n_ineq,
1938 1 + total);
1939 if (wraps_init(&wraps, mat, info, i, j) < 0)
1940 goto error;
1941 bound = isl_vec_alloc(ctx, 1 + total);
1942 if (!set_i || !set_j || !bound)
1943 goto error;
1945 if (k % 2 == 0)
1946 isl_seq_neg(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1947 else
1948 isl_seq_cpy(bound->el, info[i].bmap->eq[k / 2], 1 + total);
1949 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1951 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1952 wraps.mat->n_row = 1;
1954 if (add_wraps(&wraps, &info[j], bound->el, set_i) < 0)
1955 goto error;
1956 if (!wraps.mat->n_row)
1957 goto unbounded;
1959 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1960 isl_seq_neg(bound->el, bound->el, 1 + total);
1962 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1963 wraps.mat->n_row++;
1965 if (add_wraps(&wraps, &info[i], bound->el, set_j) < 0)
1966 goto error;
1967 if (!wraps.mat->n_row)
1968 goto unbounded;
1970 change = fuse(i, j, info, wraps.mat, detect_equalities, 0);
1972 if (0) {
1973 error: change = isl_change_error;
1975 unbounded:
1977 wraps_free(&wraps);
1978 isl_set_free(set_i);
1979 isl_set_free(set_j);
1980 isl_vec_free(bound);
1982 return change;
1985 /* Initialize the "eq" and "ineq" fields of "info".
1987 static void init_status(struct isl_coalesce_info *info)
1989 info->eq = info->ineq = NULL;
1992 /* Set info->eq to the positions of the equalities of info->bmap
1993 * with respect to the basic map represented by "tab".
1994 * If info->eq has already been computed, then do not compute it again.
1996 static void set_eq_status_in(struct isl_coalesce_info *info,
1997 struct isl_tab *tab)
1999 if (info->eq)
2000 return;
2001 info->eq = eq_status_in(info->bmap, tab);
2004 /* Set info->ineq to the positions of the inequalities of info->bmap
2005 * with respect to the basic map represented by "tab".
2006 * If info->ineq has already been computed, then do not compute it again.
2008 static void set_ineq_status_in(struct isl_coalesce_info *info,
2009 struct isl_tab *tab)
2011 if (info->ineq)
2012 return;
2013 info->ineq = ineq_status_in(info->bmap, info->tab, tab);
2016 /* Free the memory allocated by the "eq" and "ineq" fields of "info".
2017 * This function assumes that init_status has been called on "info" first,
2018 * after which the "eq" and "ineq" fields may or may not have been
2019 * assigned a newly allocated array.
2021 static void clear_status(struct isl_coalesce_info *info)
2023 free(info->eq);
2024 free(info->ineq);
2027 /* Are all inequality constraints of the basic map represented by "info"
2028 * valid for the other basic map, except for a single constraint
2029 * that is adjacent to an inequality constraint of the other basic map?
2031 static int all_ineq_valid_or_single_adj_ineq(struct isl_coalesce_info *info)
2033 int i;
2034 int k = -1;
2036 for (i = 0; i < info->bmap->n_ineq; ++i) {
2037 if (info->ineq[i] == STATUS_REDUNDANT)
2038 continue;
2039 if (info->ineq[i] == STATUS_VALID)
2040 continue;
2041 if (info->ineq[i] != STATUS_ADJ_INEQ)
2042 return 0;
2043 if (k != -1)
2044 return 0;
2045 k = i;
2048 return k != -1;
2051 /* Basic map "i" has one or more equality constraints that separate it
2052 * from basic map "j". Check if it happens to be an extension
2053 * of basic map "j".
2054 * In particular, check that all constraints of "j" are valid for "i",
2055 * except for one inequality constraint that is adjacent
2056 * to an inequality constraints of "i".
2057 * If so, check for "i" being an extension of "j" by calling
2058 * is_adj_ineq_extension.
2060 * Clean up the memory allocated for keeping track of the status
2061 * of the constraints before returning.
2063 static enum isl_change separating_equality(int i, int j,
2064 struct isl_coalesce_info *info)
2066 enum isl_change change = isl_change_none;
2068 if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2069 all_ineq_valid_or_single_adj_ineq(&info[j]))
2070 change = is_adj_ineq_extension(j, i, info);
2072 clear_status(&info[i]);
2073 clear_status(&info[j]);
2074 return change;
2077 /* Check if the union of the given pair of basic maps
2078 * can be represented by a single basic map.
2079 * If so, replace the pair by the single basic map and return
2080 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2081 * Otherwise, return isl_change_none.
2082 * The two basic maps are assumed to live in the same local space.
2083 * The "eq" and "ineq" fields of info[i] and info[j] are assumed
2084 * to have been initialized by the caller, either to NULL or
2085 * to valid information.
2087 * We first check the effect of each constraint of one basic map
2088 * on the other basic map.
2089 * The constraint may be
2090 * redundant the constraint is redundant in its own
2091 * basic map and should be ignore and removed
2092 * in the end
2093 * valid all (integer) points of the other basic map
2094 * satisfy the constraint
2095 * separate no (integer) point of the other basic map
2096 * satisfies the constraint
2097 * cut some but not all points of the other basic map
2098 * satisfy the constraint
2099 * adj_eq the given constraint is adjacent (on the outside)
2100 * to an equality of the other basic map
2101 * adj_ineq the given constraint is adjacent (on the outside)
2102 * to an inequality of the other basic map
2104 * We consider seven cases in which we can replace the pair by a single
2105 * basic map. We ignore all "redundant" constraints.
2107 * 1. all constraints of one basic map are valid
2108 * => the other basic map is a subset and can be removed
2110 * 2. all constraints of both basic maps are either "valid" or "cut"
2111 * and the facets corresponding to the "cut" constraints
2112 * of one of the basic maps lies entirely inside the other basic map
2113 * => the pair can be replaced by a basic map consisting
2114 * of the valid constraints in both basic maps
2116 * 3. there is a single pair of adjacent inequalities
2117 * (all other constraints are "valid")
2118 * => the pair can be replaced by a basic map consisting
2119 * of the valid constraints in both basic maps
2121 * 4. one basic map has a single adjacent inequality, while the other
2122 * constraints are "valid". The other basic map has some
2123 * "cut" constraints, but replacing the adjacent inequality by
2124 * its opposite and adding the valid constraints of the other
2125 * basic map results in a subset of the other basic map
2126 * => the pair can be replaced by a basic map consisting
2127 * of the valid constraints in both basic maps
2129 * 5. there is a single adjacent pair of an inequality and an equality,
2130 * the other constraints of the basic map containing the inequality are
2131 * "valid". Moreover, if the inequality the basic map is relaxed
2132 * and then turned into an equality, then resulting facet lies
2133 * entirely inside the other basic map
2134 * => the pair can be replaced by the basic map containing
2135 * the inequality, with the inequality relaxed.
2137 * 6. there is a single adjacent pair of an inequality and an equality,
2138 * the other constraints of the basic map containing the inequality are
2139 * "valid". Moreover, the facets corresponding to both
2140 * the inequality and the equality can be wrapped around their
2141 * ridges to include the other basic map
2142 * => the pair can be replaced by a basic map consisting
2143 * of the valid constraints in both basic maps together
2144 * with all wrapping constraints
2146 * 7. one of the basic maps extends beyond the other by at most one.
2147 * Moreover, the facets corresponding to the cut constraints and
2148 * the pieces of the other basic map at offset one from these cut
2149 * constraints can be wrapped around their ridges to include
2150 * the union of the two basic maps
2151 * => the pair can be replaced by a basic map consisting
2152 * of the valid constraints in both basic maps together
2153 * with all wrapping constraints
2155 * 8. the two basic maps live in adjacent hyperplanes. In principle
2156 * such sets can always be combined through wrapping, but we impose
2157 * that there is only one such pair, to avoid overeager coalescing.
2159 * Throughout the computation, we maintain a collection of tableaus
2160 * corresponding to the basic maps. When the basic maps are dropped
2161 * or combined, the tableaus are modified accordingly.
2163 static enum isl_change coalesce_local_pair_reuse(int i, int j,
2164 struct isl_coalesce_info *info)
2166 enum isl_change change = isl_change_none;
2168 set_ineq_status_in(&info[i], info[j].tab);
2169 if (info[i].bmap->n_ineq && !info[i].ineq)
2170 goto error;
2171 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ERROR))
2172 goto error;
2173 if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_SEPARATE))
2174 goto done;
2176 set_ineq_status_in(&info[j], info[i].tab);
2177 if (info[j].bmap->n_ineq && !info[j].ineq)
2178 goto error;
2179 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ERROR))
2180 goto error;
2181 if (any(info[j].ineq, info[j].bmap->n_ineq, STATUS_SEPARATE))
2182 goto done;
2184 set_eq_status_in(&info[i], info[j].tab);
2185 if (info[i].bmap->n_eq && !info[i].eq)
2186 goto error;
2187 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ERROR))
2188 goto error;
2190 set_eq_status_in(&info[j], info[i].tab);
2191 if (info[j].bmap->n_eq && !info[j].eq)
2192 goto error;
2193 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ERROR))
2194 goto error;
2196 if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_SEPARATE))
2197 return separating_equality(i, j, info);
2198 if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_SEPARATE))
2199 return separating_equality(j, i, info);
2201 if (all(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_VALID) &&
2202 all(info[i].ineq, info[i].bmap->n_ineq, STATUS_VALID)) {
2203 drop(&info[j]);
2204 change = isl_change_drop_second;
2205 } else if (all(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_VALID) &&
2206 all(info[j].ineq, info[j].bmap->n_ineq, STATUS_VALID)) {
2207 drop(&info[i]);
2208 change = isl_change_drop_first;
2209 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_EQ)) {
2210 change = check_eq_adj_eq(i, j, info);
2211 } else if (any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_EQ)) {
2212 change = check_eq_adj_eq(j, i, info);
2213 } else if (any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_ADJ_INEQ) ||
2214 any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_ADJ_INEQ)) {
2215 change = check_adj_eq(i, j, info);
2216 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_EQ) ||
2217 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_EQ)) {
2218 /* Can't happen */
2219 /* BAD ADJ INEQ */
2220 } else if (any(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ) ||
2221 any(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ)) {
2222 change = check_adj_ineq(i, j, info);
2223 } else {
2224 if (!any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) &&
2225 !any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT))
2226 change = check_facets(i, j, info);
2227 if (change == isl_change_none)
2228 change = check_wrap(i, j, info);
2231 done:
2232 clear_status(&info[i]);
2233 clear_status(&info[j]);
2234 return change;
2235 error:
2236 clear_status(&info[i]);
2237 clear_status(&info[j]);
2238 return isl_change_error;
2241 /* Check if the union of the given pair of basic maps
2242 * can be represented by a single basic map.
2243 * If so, replace the pair by the single basic map and return
2244 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2245 * Otherwise, return isl_change_none.
2246 * The two basic maps are assumed to live in the same local space.
2248 static enum isl_change coalesce_local_pair(int i, int j,
2249 struct isl_coalesce_info *info)
2251 init_status(&info[i]);
2252 init_status(&info[j]);
2253 return coalesce_local_pair_reuse(i, j, info);
2256 /* Shift the integer division at position "div" of the basic map
2257 * represented by "info" by "shift".
2259 * That is, if the integer division has the form
2261 * floor(f(x)/d)
2263 * then replace it by
2265 * floor((f(x) + shift * d)/d) - shift
2267 static isl_stat shift_div(struct isl_coalesce_info *info, int div,
2268 isl_int shift)
2270 unsigned total;
2272 info->bmap = isl_basic_map_shift_div(info->bmap, div, 0, shift);
2273 if (!info->bmap)
2274 return isl_stat_error;
2276 total = isl_basic_map_dim(info->bmap, isl_dim_all);
2277 total -= isl_basic_map_dim(info->bmap, isl_dim_div);
2278 if (isl_tab_shift_var(info->tab, total + div, shift) < 0)
2279 return isl_stat_error;
2281 return isl_stat_ok;
2284 /* If the integer division at position "div" is defined by an equality,
2285 * i.e., a stride constraint, then change the integer division expression
2286 * to have a constant term equal to zero.
2288 * Let the equality constraint be
2290 * c + f + m a = 0
2292 * The integer division expression is then of the form
2294 * a = floor((-f - c')/m)
2296 * The integer division is first shifted by t = floor(c/m),
2297 * turning the equality constraint into
2299 * c - m floor(c/m) + f + m a' = 0
2301 * i.e.,
2303 * (c mod m) + f + m a' = 0
2305 * That is,
2307 * a' = (-f - (c mod m))/m = floor((-f)/m)
2309 * because a' is an integer and 0 <= (c mod m) < m.
2310 * The constant term of a' can therefore be zeroed out.
2312 static isl_stat normalize_stride_div(struct isl_coalesce_info *info, int div)
2314 isl_bool defined;
2315 isl_stat r;
2316 isl_constraint *c;
2317 isl_int shift, stride;
2319 defined = isl_basic_map_has_defining_equality(info->bmap, isl_dim_div,
2320 div, &c);
2321 if (defined < 0)
2322 return isl_stat_error;
2323 if (!defined)
2324 return isl_stat_ok;
2325 if (!c)
2326 return isl_stat_error;
2327 isl_int_init(shift);
2328 isl_int_init(stride);
2329 isl_constraint_get_constant(c, &shift);
2330 isl_constraint_get_coefficient(c, isl_dim_div, div, &stride);
2331 isl_int_fdiv_q(shift, shift, stride);
2332 r = shift_div(info, div, shift);
2333 isl_int_clear(stride);
2334 isl_int_clear(shift);
2335 isl_constraint_free(c);
2336 if (r < 0)
2337 return isl_stat_error;
2338 info->bmap = isl_basic_map_set_div_expr_constant_num_si_inplace(
2339 info->bmap, div, 0);
2340 if (!info->bmap)
2341 return isl_stat_error;
2342 return isl_stat_ok;
2345 /* The basic maps represented by "info1" and "info2" are known
2346 * to have the same number of integer divisions.
2347 * Check if pairs of integer divisions are equal to each other
2348 * despite the fact that they differ by a rational constant.
2350 * In particular, look for any pair of integer divisions that
2351 * only differ in their constant terms.
2352 * If either of these integer divisions is defined
2353 * by stride constraints, then modify it to have a zero constant term.
2354 * If both are defined by stride constraints then in the end they will have
2355 * the same (zero) constant term.
2357 static isl_stat harmonize_stride_divs(struct isl_coalesce_info *info1,
2358 struct isl_coalesce_info *info2)
2360 int i, n;
2362 n = isl_basic_map_dim(info1->bmap, isl_dim_div);
2363 for (i = 0; i < n; ++i) {
2364 isl_bool known, harmonize;
2366 known = isl_basic_map_div_is_known(info1->bmap, i);
2367 if (known >= 0 && known)
2368 known = isl_basic_map_div_is_known(info2->bmap, i);
2369 if (known < 0)
2370 return isl_stat_error;
2371 if (!known)
2372 continue;
2373 harmonize = isl_basic_map_equal_div_expr_except_constant(
2374 info1->bmap, i, info2->bmap, i);
2375 if (harmonize < 0)
2376 return isl_stat_error;
2377 if (!harmonize)
2378 continue;
2379 if (normalize_stride_div(info1, i) < 0)
2380 return isl_stat_error;
2381 if (normalize_stride_div(info2, i) < 0)
2382 return isl_stat_error;
2385 return isl_stat_ok;
2388 /* If "shift" is an integer constant, then shift the integer division
2389 * at position "div" of the basic map represented by "info" by "shift".
2390 * If "shift" is not an integer constant, then do nothing.
2391 * If "shift" is equal to zero, then no shift needs to be performed either.
2393 * That is, if the integer division has the form
2395 * floor(f(x)/d)
2397 * then replace it by
2399 * floor((f(x) + shift * d)/d) - shift
2401 static isl_stat shift_if_cst_int(struct isl_coalesce_info *info, int div,
2402 __isl_keep isl_aff *shift)
2404 isl_bool cst;
2405 isl_stat r;
2406 isl_int d;
2407 isl_val *c;
2409 cst = isl_aff_is_cst(shift);
2410 if (cst < 0 || !cst)
2411 return cst < 0 ? isl_stat_error : isl_stat_ok;
2413 c = isl_aff_get_constant_val(shift);
2414 cst = isl_val_is_int(c);
2415 if (cst >= 0 && cst)
2416 cst = isl_bool_not(isl_val_is_zero(c));
2417 if (cst < 0 || !cst) {
2418 isl_val_free(c);
2419 return cst < 0 ? isl_stat_error : isl_stat_ok;
2422 isl_int_init(d);
2423 r = isl_val_get_num_isl_int(c, &d);
2424 if (r >= 0)
2425 r = shift_div(info, div, d);
2426 isl_int_clear(d);
2428 isl_val_free(c);
2430 return r;
2433 /* Check if some of the divs in the basic map represented by "info1"
2434 * are shifts of the corresponding divs in the basic map represented
2435 * by "info2", taking into account the equality constraints "eq1" of "info1"
2436 * and "eq2" of "info2". If so, align them with those of "info2".
2437 * "info1" and "info2" are assumed to have the same number
2438 * of integer divisions.
2440 * An integer division is considered to be a shift of another integer
2441 * division if, after simplification with respect to the equality
2442 * constraints of the other basic map, one is equal to the other
2443 * plus a constant.
2445 * In particular, for each pair of integer divisions, if both are known,
2446 * have the same denominator and are not already equal to each other,
2447 * simplify each with respect to the equality constraints
2448 * of the other basic map. If the difference is an integer constant,
2449 * then move this difference outside.
2450 * That is, if, after simplification, one integer division is of the form
2452 * floor((f(x) + c_1)/d)
2454 * while the other is of the form
2456 * floor((f(x) + c_2)/d)
2458 * and n = (c_2 - c_1)/d is an integer, then replace the first
2459 * integer division by
2461 * floor((f_1(x) + c_1 + n * d)/d) - n,
2463 * where floor((f_1(x) + c_1 + n * d)/d) = floor((f2(x) + c_2)/d)
2464 * after simplification with respect to the equality constraints.
2466 static isl_stat harmonize_divs_with_hulls(struct isl_coalesce_info *info1,
2467 struct isl_coalesce_info *info2, __isl_keep isl_basic_set *eq1,
2468 __isl_keep isl_basic_set *eq2)
2470 int i;
2471 int total;
2472 isl_local_space *ls1, *ls2;
2474 total = isl_basic_map_total_dim(info1->bmap);
2475 ls1 = isl_local_space_wrap(isl_basic_map_get_local_space(info1->bmap));
2476 ls2 = isl_local_space_wrap(isl_basic_map_get_local_space(info2->bmap));
2477 for (i = 0; i < info1->bmap->n_div; ++i) {
2478 isl_stat r;
2479 isl_aff *div1, *div2;
2481 if (!isl_local_space_div_is_known(ls1, i) ||
2482 !isl_local_space_div_is_known(ls2, i))
2483 continue;
2484 if (isl_int_ne(info1->bmap->div[i][0], info2->bmap->div[i][0]))
2485 continue;
2486 if (isl_seq_eq(info1->bmap->div[i] + 1,
2487 info2->bmap->div[i] + 1, 1 + total))
2488 continue;
2489 div1 = isl_local_space_get_div(ls1, i);
2490 div2 = isl_local_space_get_div(ls2, i);
2491 div1 = isl_aff_substitute_equalities(div1,
2492 isl_basic_set_copy(eq2));
2493 div2 = isl_aff_substitute_equalities(div2,
2494 isl_basic_set_copy(eq1));
2495 div2 = isl_aff_sub(div2, div1);
2496 r = shift_if_cst_int(info1, i, div2);
2497 isl_aff_free(div2);
2498 if (r < 0)
2499 break;
2501 isl_local_space_free(ls1);
2502 isl_local_space_free(ls2);
2504 if (i < info1->bmap->n_div)
2505 return isl_stat_error;
2506 return isl_stat_ok;
2509 /* Check if some of the divs in the basic map represented by "info1"
2510 * are shifts of the corresponding divs in the basic map represented
2511 * by "info2". If so, align them with those of "info2".
2512 * Only do this if "info1" and "info2" have the same number
2513 * of integer divisions.
2515 * An integer division is considered to be a shift of another integer
2516 * division if, after simplification with respect to the equality
2517 * constraints of the other basic map, one is equal to the other
2518 * plus a constant.
2520 * First check if pairs of integer divisions are equal to each other
2521 * despite the fact that they differ by a rational constant.
2522 * If so, try and arrange for them to have the same constant term.
2524 * Then, extract the equality constraints and continue with
2525 * harmonize_divs_with_hulls.
2527 * If the equality constraints of both basic maps are the same,
2528 * then there is no need to perform any shifting since
2529 * the coefficients of the integer divisions should have been
2530 * reduced in the same way.
2532 static isl_stat harmonize_divs(struct isl_coalesce_info *info1,
2533 struct isl_coalesce_info *info2)
2535 isl_bool equal;
2536 isl_basic_map *bmap1, *bmap2;
2537 isl_basic_set *eq1, *eq2;
2538 isl_stat r;
2540 if (!info1->bmap || !info2->bmap)
2541 return isl_stat_error;
2543 if (info1->bmap->n_div != info2->bmap->n_div)
2544 return isl_stat_ok;
2545 if (info1->bmap->n_div == 0)
2546 return isl_stat_ok;
2548 if (harmonize_stride_divs(info1, info2) < 0)
2549 return isl_stat_error;
2551 bmap1 = isl_basic_map_copy(info1->bmap);
2552 bmap2 = isl_basic_map_copy(info2->bmap);
2553 eq1 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap1));
2554 eq2 = isl_basic_map_wrap(isl_basic_map_plain_affine_hull(bmap2));
2555 equal = isl_basic_set_plain_is_equal(eq1, eq2);
2556 if (equal < 0)
2557 r = isl_stat_error;
2558 else if (equal)
2559 r = isl_stat_ok;
2560 else
2561 r = harmonize_divs_with_hulls(info1, info2, eq1, eq2);
2562 isl_basic_set_free(eq1);
2563 isl_basic_set_free(eq2);
2565 return r;
2568 /* Do the two basic maps live in the same local space, i.e.,
2569 * do they have the same (known) divs?
2570 * If either basic map has any unknown divs, then we can only assume
2571 * that they do not live in the same local space.
2573 static isl_bool same_divs(__isl_keep isl_basic_map *bmap1,
2574 __isl_keep isl_basic_map *bmap2)
2576 int i;
2577 isl_bool known;
2578 int total;
2580 if (!bmap1 || !bmap2)
2581 return isl_bool_error;
2582 if (bmap1->n_div != bmap2->n_div)
2583 return isl_bool_false;
2585 if (bmap1->n_div == 0)
2586 return isl_bool_true;
2588 known = isl_basic_map_divs_known(bmap1);
2589 if (known < 0 || !known)
2590 return known;
2591 known = isl_basic_map_divs_known(bmap2);
2592 if (known < 0 || !known)
2593 return known;
2595 total = isl_basic_map_total_dim(bmap1);
2596 for (i = 0; i < bmap1->n_div; ++i)
2597 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
2598 return 0;
2600 return 1;
2603 /* Assuming that "tab" contains the equality constraints and
2604 * the initial inequality constraints of "bmap", copy the remaining
2605 * inequality constraints of "bmap" to "Tab".
2607 static isl_stat copy_ineq(struct isl_tab *tab, __isl_keep isl_basic_map *bmap)
2609 int i, n_ineq;
2611 if (!bmap)
2612 return isl_stat_error;
2614 n_ineq = tab->n_con - tab->n_eq;
2615 for (i = n_ineq; i < bmap->n_ineq; ++i)
2616 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2617 return isl_stat_error;
2619 return isl_stat_ok;
2622 /* Description of an integer division that is added
2623 * during an expansion.
2624 * "pos" is the position of the corresponding variable.
2625 * "cst" indicates whether this integer division has a fixed value.
2626 * "val" contains the fixed value, if the value is fixed.
2628 struct isl_expanded {
2629 int pos;
2630 isl_bool cst;
2631 isl_int val;
2634 /* For each of the "n" integer division variables "expanded",
2635 * if the variable has a fixed value, then add two inequality
2636 * constraints expressing the fixed value.
2637 * Otherwise, add the corresponding div constraints.
2638 * The caller is responsible for removing the div constraints
2639 * that it added for all these "n" integer divisions.
2641 * The div constraints and the pair of inequality constraints
2642 * forcing the fixed value cannot both be added for a given variable
2643 * as the combination may render some of the original constraints redundant.
2644 * These would then be ignored during the coalescing detection,
2645 * while they could remain in the fused result.
2647 * The two added inequality constraints are
2649 * -a + v >= 0
2650 * a - v >= 0
2652 * with "a" the variable and "v" its fixed value.
2653 * The facet corresponding to one of these two constraints is selected
2654 * in the tableau to ensure that the pair of inequality constraints
2655 * is treated as an equality constraint.
2657 * The information in info->ineq is thrown away because it was
2658 * computed in terms of div constraints, while some of those
2659 * have now been replaced by these pairs of inequality constraints.
2661 static isl_stat fix_constant_divs(struct isl_coalesce_info *info,
2662 int n, struct isl_expanded *expanded)
2664 unsigned o_div;
2665 int i;
2666 isl_vec *ineq;
2668 o_div = isl_basic_map_offset(info->bmap, isl_dim_div) - 1;
2669 ineq = isl_vec_alloc(isl_tab_get_ctx(info->tab), 1 + info->tab->n_var);
2670 if (!ineq)
2671 return isl_stat_error;
2672 isl_seq_clr(ineq->el + 1, info->tab->n_var);
2674 for (i = 0; i < n; ++i) {
2675 if (!expanded[i].cst) {
2676 info->bmap = isl_basic_map_extend_constraints(
2677 info->bmap, 0, 2);
2678 if (isl_basic_map_add_div_constraints(info->bmap,
2679 expanded[i].pos - o_div) < 0)
2680 break;
2681 } else {
2682 isl_int_set_si(ineq->el[1 + expanded[i].pos], -1);
2683 isl_int_set(ineq->el[0], expanded[i].val);
2684 info->bmap = isl_basic_map_add_ineq(info->bmap,
2685 ineq->el);
2686 isl_int_set_si(ineq->el[1 + expanded[i].pos], 1);
2687 isl_int_neg(ineq->el[0], expanded[i].val);
2688 info->bmap = isl_basic_map_add_ineq(info->bmap,
2689 ineq->el);
2690 isl_int_set_si(ineq->el[1 + expanded[i].pos], 0);
2692 if (copy_ineq(info->tab, info->bmap) < 0)
2693 break;
2694 if (expanded[i].cst &&
2695 isl_tab_select_facet(info->tab, info->tab->n_con - 1) < 0)
2696 break;
2699 isl_vec_free(ineq);
2701 clear_status(info);
2702 init_status(info);
2704 return i < n ? isl_stat_error : isl_stat_ok;
2707 /* Insert the "n" integer division variables "expanded"
2708 * into info->tab and info->bmap and
2709 * update info->ineq with respect to the redundant constraints
2710 * in the resulting tableau.
2711 * "bmap" contains the result of this insertion in info->bmap,
2712 * while info->bmap is the original version
2713 * of "bmap", i.e., the one that corresponds to the current
2714 * state of info->tab. The number of constraints in info->bmap
2715 * is assumed to be the same as the number of constraints
2716 * in info->tab. This is required to be able to detect
2717 * the extra constraints in "bmap".
2719 * In particular, introduce extra variables corresponding
2720 * to the extra integer divisions and add the div constraints
2721 * that were added to "bmap" after info->tab was created
2722 * from info->bmap.
2723 * Furthermore, check if these extra integer divisions happen
2724 * to attain a fixed integer value in info->tab.
2725 * If so, replace the corresponding div constraints by pairs
2726 * of inequality constraints that fix these
2727 * integer divisions to their single integer values.
2728 * Replace info->bmap by "bmap" to match the changes to info->tab.
2729 * info->ineq was computed without a tableau and therefore
2730 * does not take into account the redundant constraints
2731 * in the tableau. Mark them here.
2732 * There is no need to check the newly added div constraints
2733 * since they cannot be redundant.
2734 * The redundancy check is not performed when constants have been discovered
2735 * since info->ineq is completely thrown away in this case.
2737 static isl_stat tab_insert_divs(struct isl_coalesce_info *info,
2738 int n, struct isl_expanded *expanded, __isl_take isl_basic_map *bmap)
2740 int i, n_ineq;
2741 unsigned n_eq;
2742 struct isl_tab_undo *snap;
2743 int any;
2745 if (!bmap)
2746 return isl_stat_error;
2747 if (info->bmap->n_eq + info->bmap->n_ineq != info->tab->n_con)
2748 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2749 "original tableau does not correspond "
2750 "to original basic map", goto error);
2752 if (isl_tab_extend_vars(info->tab, n) < 0)
2753 goto error;
2754 if (isl_tab_extend_cons(info->tab, 2 * n) < 0)
2755 goto error;
2757 for (i = 0; i < n; ++i) {
2758 if (isl_tab_insert_var(info->tab, expanded[i].pos) < 0)
2759 goto error;
2762 snap = isl_tab_snap(info->tab);
2764 n_ineq = info->tab->n_con - info->tab->n_eq;
2765 if (copy_ineq(info->tab, bmap) < 0)
2766 goto error;
2768 isl_basic_map_free(info->bmap);
2769 info->bmap = bmap;
2771 any = 0;
2772 for (i = 0; i < n; ++i) {
2773 expanded[i].cst = isl_tab_is_constant(info->tab,
2774 expanded[i].pos, &expanded[i].val);
2775 if (expanded[i].cst < 0)
2776 return isl_stat_error;
2777 if (expanded[i].cst)
2778 any = 1;
2781 if (any) {
2782 if (isl_tab_rollback(info->tab, snap) < 0)
2783 return isl_stat_error;
2784 info->bmap = isl_basic_map_cow(info->bmap);
2785 if (isl_basic_map_free_inequality(info->bmap, 2 * n) < 0)
2786 return isl_stat_error;
2788 return fix_constant_divs(info, n, expanded);
2791 n_eq = info->bmap->n_eq;
2792 for (i = 0; i < n_ineq; ++i) {
2793 if (isl_tab_is_redundant(info->tab, n_eq + i))
2794 info->ineq[i] = STATUS_REDUNDANT;
2797 return isl_stat_ok;
2798 error:
2799 isl_basic_map_free(bmap);
2800 return isl_stat_error;
2803 /* Expand info->tab and info->bmap in the same way "bmap" was expanded
2804 * in isl_basic_map_expand_divs using the expansion "exp" and
2805 * update info->ineq with respect to the redundant constraints
2806 * in the resulting tableau. info->bmap is the original version
2807 * of "bmap", i.e., the one that corresponds to the current
2808 * state of info->tab. The number of constraints in info->bmap
2809 * is assumed to be the same as the number of constraints
2810 * in info->tab. This is required to be able to detect
2811 * the extra constraints in "bmap".
2813 * Extract the positions where extra local variables are introduced
2814 * from "exp" and call tab_insert_divs.
2816 static isl_stat expand_tab(struct isl_coalesce_info *info, int *exp,
2817 __isl_take isl_basic_map *bmap)
2819 isl_ctx *ctx;
2820 struct isl_expanded *expanded;
2821 int i, j, k, n;
2822 int extra_var;
2823 unsigned total, pos, n_div;
2824 isl_stat r;
2826 total = isl_basic_map_dim(bmap, isl_dim_all);
2827 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2828 pos = total - n_div;
2829 extra_var = total - info->tab->n_var;
2830 n = n_div - extra_var;
2832 ctx = isl_basic_map_get_ctx(bmap);
2833 expanded = isl_calloc_array(ctx, struct isl_expanded, extra_var);
2834 if (extra_var && !expanded)
2835 goto error;
2837 i = 0;
2838 k = 0;
2839 for (j = 0; j < n_div; ++j) {
2840 if (i < n && exp[i] == j) {
2841 ++i;
2842 continue;
2844 expanded[k++].pos = pos + j;
2847 for (k = 0; k < extra_var; ++k)
2848 isl_int_init(expanded[k].val);
2850 r = tab_insert_divs(info, extra_var, expanded, bmap);
2852 for (k = 0; k < extra_var; ++k)
2853 isl_int_clear(expanded[k].val);
2854 free(expanded);
2856 return r;
2857 error:
2858 isl_basic_map_free(bmap);
2859 return isl_stat_error;
2862 /* Check if the union of the basic maps represented by info[i] and info[j]
2863 * can be represented by a single basic map,
2864 * after expanding the divs of info[i] to match those of info[j].
2865 * If so, replace the pair by the single basic map and return
2866 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2867 * Otherwise, return isl_change_none.
2869 * The caller has already checked for info[j] being a subset of info[i].
2870 * If some of the divs of info[j] are unknown, then the expanded info[i]
2871 * will not have the corresponding div constraints. The other patterns
2872 * therefore cannot apply. Skip the computation in this case.
2874 * The expansion is performed using the divs "div" and expansion "exp"
2875 * computed by the caller.
2876 * info[i].bmap has already been expanded and the result is passed in
2877 * as "bmap".
2878 * The "eq" and "ineq" fields of info[i] reflect the status of
2879 * the constraints of the expanded "bmap" with respect to info[j].tab.
2880 * However, inequality constraints that are redundant in info[i].tab
2881 * have not yet been marked as such because no tableau was available.
2883 * Replace info[i].bmap by "bmap" and expand info[i].tab as well,
2884 * updating info[i].ineq with respect to the redundant constraints.
2885 * Then try and coalesce the expanded info[i] with info[j],
2886 * reusing the information in info[i].eq and info[i].ineq.
2887 * If this does not result in any coalescing or if it results in info[j]
2888 * getting dropped (which should not happen in practice, since the case
2889 * of info[j] being a subset of info[i] has already been checked by
2890 * the caller), then revert info[i] to its original state.
2892 static enum isl_change coalesce_expand_tab_divs(__isl_take isl_basic_map *bmap,
2893 int i, int j, struct isl_coalesce_info *info, __isl_keep isl_mat *div,
2894 int *exp)
2896 isl_bool known;
2897 isl_basic_map *bmap_i;
2898 struct isl_tab_undo *snap;
2899 enum isl_change change = isl_change_none;
2901 known = isl_basic_map_divs_known(info[j].bmap);
2902 if (known < 0 || !known) {
2903 clear_status(&info[i]);
2904 isl_basic_map_free(bmap);
2905 return known < 0 ? isl_change_error : isl_change_none;
2908 bmap_i = isl_basic_map_copy(info[i].bmap);
2909 snap = isl_tab_snap(info[i].tab);
2910 if (expand_tab(&info[i], exp, bmap) < 0)
2911 change = isl_change_error;
2913 init_status(&info[j]);
2914 if (change == isl_change_none)
2915 change = coalesce_local_pair_reuse(i, j, info);
2916 else
2917 clear_status(&info[i]);
2918 if (change != isl_change_none && change != isl_change_drop_second) {
2919 isl_basic_map_free(bmap_i);
2920 } else {
2921 isl_basic_map_free(info[i].bmap);
2922 info[i].bmap = bmap_i;
2924 if (isl_tab_rollback(info[i].tab, snap) < 0)
2925 change = isl_change_error;
2928 return change;
2931 /* Check if the union of "bmap" and the basic map represented by info[j]
2932 * can be represented by a single basic map,
2933 * after expanding the divs of "bmap" to match those of info[j].
2934 * If so, replace the pair by the single basic map and return
2935 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
2936 * Otherwise, return isl_change_none.
2938 * In particular, check if the expanded "bmap" contains the basic map
2939 * represented by the tableau info[j].tab.
2940 * The expansion is performed using the divs "div" and expansion "exp"
2941 * computed by the caller.
2942 * Then we check if all constraints of the expanded "bmap" are valid for
2943 * info[j].tab.
2945 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
2946 * In this case, the positions of the constraints of info[i].bmap
2947 * with respect to the basic map represented by info[j] are stored
2948 * in info[i].
2950 * If the expanded "bmap" does not contain the basic map
2951 * represented by the tableau info[j].tab and if "i" is not -1,
2952 * i.e., if the original "bmap" is info[i].bmap, then expand info[i].tab
2953 * as well and check if that results in coalescing.
2955 static enum isl_change coalesce_with_expanded_divs(
2956 __isl_keep isl_basic_map *bmap, int i, int j,
2957 struct isl_coalesce_info *info, __isl_keep isl_mat *div, int *exp)
2959 enum isl_change change = isl_change_none;
2960 struct isl_coalesce_info info_local, *info_i;
2962 info_i = i >= 0 ? &info[i] : &info_local;
2963 init_status(info_i);
2964 bmap = isl_basic_map_copy(bmap);
2965 bmap = isl_basic_map_expand_divs(bmap, isl_mat_copy(div), exp);
2966 bmap = isl_basic_map_mark_final(bmap);
2968 if (!bmap)
2969 goto error;
2971 info_i->eq = eq_status_in(bmap, info[j].tab);
2972 if (bmap->n_eq && !info_i->eq)
2973 goto error;
2974 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_ERROR))
2975 goto error;
2976 if (any(info_i->eq, 2 * bmap->n_eq, STATUS_SEPARATE))
2977 goto done;
2979 info_i->ineq = ineq_status_in(bmap, NULL, info[j].tab);
2980 if (bmap->n_ineq && !info_i->ineq)
2981 goto error;
2982 if (any(info_i->ineq, bmap->n_ineq, STATUS_ERROR))
2983 goto error;
2984 if (any(info_i->ineq, bmap->n_ineq, STATUS_SEPARATE))
2985 goto done;
2987 if (all(info_i->eq, 2 * bmap->n_eq, STATUS_VALID) &&
2988 all(info_i->ineq, bmap->n_ineq, STATUS_VALID)) {
2989 drop(&info[j]);
2990 change = isl_change_drop_second;
2993 if (change == isl_change_none && i != -1)
2994 return coalesce_expand_tab_divs(bmap, i, j, info, div, exp);
2996 done:
2997 isl_basic_map_free(bmap);
2998 clear_status(info_i);
2999 return change;
3000 error:
3001 isl_basic_map_free(bmap);
3002 clear_status(info_i);
3003 return isl_change_error;
3006 /* Check if the union of "bmap_i" and the basic map represented by info[j]
3007 * can be represented by a single basic map,
3008 * after aligning the divs of "bmap_i" to match those of info[j].
3009 * If so, replace the pair by the single basic map and return
3010 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3011 * Otherwise, return isl_change_none.
3013 * In particular, check if "bmap_i" contains the basic map represented by
3014 * info[j] after aligning the divs of "bmap_i" to those of info[j].
3015 * Note that this can only succeed if the number of divs of "bmap_i"
3016 * is smaller than (or equal to) the number of divs of info[j].
3018 * We first check if the divs of "bmap_i" are all known and form a subset
3019 * of those of info[j].bmap. If so, we pass control over to
3020 * coalesce_with_expanded_divs.
3022 * If "i" is not equal to -1, then "bmap" is equal to info[i].bmap.
3024 static enum isl_change coalesce_after_aligning_divs(
3025 __isl_keep isl_basic_map *bmap_i, int i, int j,
3026 struct isl_coalesce_info *info)
3028 int known;
3029 isl_mat *div_i, *div_j, *div;
3030 int *exp1 = NULL;
3031 int *exp2 = NULL;
3032 isl_ctx *ctx;
3033 enum isl_change change;
3035 known = isl_basic_map_divs_known(bmap_i);
3036 if (known < 0 || !known)
3037 return known;
3039 ctx = isl_basic_map_get_ctx(bmap_i);
3041 div_i = isl_basic_map_get_divs(bmap_i);
3042 div_j = isl_basic_map_get_divs(info[j].bmap);
3044 if (!div_i || !div_j)
3045 goto error;
3047 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
3048 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
3049 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
3050 goto error;
3052 div = isl_merge_divs(div_i, div_j, exp1, exp2);
3053 if (!div)
3054 goto error;
3056 if (div->n_row == div_j->n_row)
3057 change = coalesce_with_expanded_divs(bmap_i,
3058 i, j, info, div, exp1);
3059 else
3060 change = isl_change_none;
3062 isl_mat_free(div);
3064 isl_mat_free(div_i);
3065 isl_mat_free(div_j);
3067 free(exp2);
3068 free(exp1);
3070 return change;
3071 error:
3072 isl_mat_free(div_i);
3073 isl_mat_free(div_j);
3074 free(exp1);
3075 free(exp2);
3076 return isl_change_error;
3079 /* Check if basic map "j" is a subset of basic map "i" after
3080 * exploiting the extra equalities of "j" to simplify the divs of "i".
3081 * If so, remove basic map "j" and return isl_change_drop_second.
3083 * If "j" does not have any equalities or if they are the same
3084 * as those of "i", then we cannot exploit them to simplify the divs.
3085 * Similarly, if there are no divs in "i", then they cannot be simplified.
3086 * If, on the other hand, the affine hulls of "i" and "j" do not intersect,
3087 * then "j" cannot be a subset of "i".
3089 * Otherwise, we intersect "i" with the affine hull of "j" and then
3090 * check if "j" is a subset of the result after aligning the divs.
3091 * If so, then "j" is definitely a subset of "i" and can be removed.
3092 * Note that if after intersection with the affine hull of "j".
3093 * "i" still has more divs than "j", then there is no way we can
3094 * align the divs of "i" to those of "j".
3096 static enum isl_change coalesce_subset_with_equalities(int i, int j,
3097 struct isl_coalesce_info *info)
3099 isl_basic_map *hull_i, *hull_j, *bmap_i;
3100 int equal, empty;
3101 enum isl_change change;
3103 if (info[j].bmap->n_eq == 0)
3104 return isl_change_none;
3105 if (info[i].bmap->n_div == 0)
3106 return isl_change_none;
3108 hull_i = isl_basic_map_copy(info[i].bmap);
3109 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3110 hull_j = isl_basic_map_copy(info[j].bmap);
3111 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3113 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3114 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3115 empty = isl_basic_map_plain_is_empty(hull_j);
3116 isl_basic_map_free(hull_i);
3118 if (equal < 0 || equal || empty < 0 || empty) {
3119 isl_basic_map_free(hull_j);
3120 if (equal < 0 || empty < 0)
3121 return isl_change_error;
3122 return isl_change_none;
3125 bmap_i = isl_basic_map_copy(info[i].bmap);
3126 bmap_i = isl_basic_map_intersect(bmap_i, hull_j);
3127 if (!bmap_i)
3128 return isl_change_error;
3130 if (bmap_i->n_div > info[j].bmap->n_div) {
3131 isl_basic_map_free(bmap_i);
3132 return isl_change_none;
3135 change = coalesce_after_aligning_divs(bmap_i, -1, j, info);
3137 isl_basic_map_free(bmap_i);
3139 return change;
3142 /* Check if the union of and the basic maps represented by info[i] and info[j]
3143 * can be represented by a single basic map, by aligning or equating
3144 * their integer divisions.
3145 * If so, replace the pair by the single basic map and return
3146 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3147 * Otherwise, return isl_change_none.
3149 * Note that we only perform any test if the number of divs is different
3150 * in the two basic maps. In case the number of divs is the same,
3151 * we have already established that the divs are different
3152 * in the two basic maps.
3153 * In particular, if the number of divs of basic map i is smaller than
3154 * the number of divs of basic map j, then we check if j is a subset of i
3155 * and vice versa.
3157 static enum isl_change coalesce_divs(int i, int j,
3158 struct isl_coalesce_info *info)
3160 enum isl_change change = isl_change_none;
3162 if (info[i].bmap->n_div < info[j].bmap->n_div)
3163 change = coalesce_after_aligning_divs(info[i].bmap, i, j, info);
3164 if (change != isl_change_none)
3165 return change;
3167 if (info[j].bmap->n_div < info[i].bmap->n_div)
3168 change = coalesce_after_aligning_divs(info[j].bmap, j, i, info);
3169 if (change != isl_change_none)
3170 return invert_change(change);
3172 change = coalesce_subset_with_equalities(i, j, info);
3173 if (change != isl_change_none)
3174 return change;
3176 change = coalesce_subset_with_equalities(j, i, info);
3177 if (change != isl_change_none)
3178 return invert_change(change);
3180 return isl_change_none;
3183 /* Does "bmap" involve any divs that themselves refer to divs?
3185 static isl_bool has_nested_div(__isl_keep isl_basic_map *bmap)
3187 int i;
3188 unsigned total;
3189 unsigned n_div;
3191 total = isl_basic_map_dim(bmap, isl_dim_all);
3192 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3193 total -= n_div;
3195 for (i = 0; i < n_div; ++i)
3196 if (isl_seq_first_non_zero(bmap->div[i] + 2 + total,
3197 n_div) != -1)
3198 return isl_bool_true;
3200 return isl_bool_false;
3203 /* Return a list of affine expressions, one for each integer division
3204 * in "bmap_i". For each integer division that also appears in "bmap_j",
3205 * the affine expression is set to NaN. The number of NaNs in the list
3206 * is equal to the number of integer divisions in "bmap_j".
3207 * For the other integer divisions of "bmap_i", the corresponding
3208 * element in the list is a purely affine expression equal to the integer
3209 * division in "hull".
3210 * If no such list can be constructed, then the number of elements
3211 * in the returned list is smaller than the number of integer divisions
3212 * in "bmap_i".
3214 static __isl_give isl_aff_list *set_up_substitutions(
3215 __isl_keep isl_basic_map *bmap_i, __isl_keep isl_basic_map *bmap_j,
3216 __isl_take isl_basic_map *hull)
3218 unsigned n_div_i, n_div_j, total;
3219 isl_ctx *ctx;
3220 isl_local_space *ls;
3221 isl_basic_set *wrap_hull;
3222 isl_aff *aff_nan;
3223 isl_aff_list *list;
3224 int i, j;
3226 if (!hull)
3227 return NULL;
3229 ctx = isl_basic_map_get_ctx(hull);
3231 n_div_i = isl_basic_map_dim(bmap_i, isl_dim_div);
3232 n_div_j = isl_basic_map_dim(bmap_j, isl_dim_div);
3233 total = isl_basic_map_total_dim(bmap_i) - n_div_i;
3235 ls = isl_basic_map_get_local_space(bmap_i);
3236 ls = isl_local_space_wrap(ls);
3237 wrap_hull = isl_basic_map_wrap(hull);
3239 aff_nan = isl_aff_nan_on_domain(isl_local_space_copy(ls));
3240 list = isl_aff_list_alloc(ctx, n_div_i);
3242 j = 0;
3243 for (i = 0; i < n_div_i; ++i) {
3244 isl_aff *aff;
3246 if (j < n_div_j &&
3247 isl_basic_map_equal_div_expr_part(bmap_i, i, bmap_j, j,
3248 0, 2 + total)) {
3249 ++j;
3250 list = isl_aff_list_add(list, isl_aff_copy(aff_nan));
3251 continue;
3253 if (n_div_i - i <= n_div_j - j)
3254 break;
3256 aff = isl_local_space_get_div(ls, i);
3257 aff = isl_aff_substitute_equalities(aff,
3258 isl_basic_set_copy(wrap_hull));
3259 aff = isl_aff_floor(aff);
3260 if (!aff)
3261 goto error;
3262 if (isl_aff_dim(aff, isl_dim_div) != 0) {
3263 isl_aff_free(aff);
3264 break;
3267 list = isl_aff_list_add(list, aff);
3270 isl_aff_free(aff_nan);
3271 isl_local_space_free(ls);
3272 isl_basic_set_free(wrap_hull);
3274 return list;
3275 error:
3276 isl_aff_free(aff_nan);
3277 isl_local_space_free(ls);
3278 isl_basic_set_free(wrap_hull);
3279 isl_aff_list_free(list);
3280 return NULL;
3283 /* Add variables to info->bmap and info->tab corresponding to the elements
3284 * in "list" that are not set to NaN.
3285 * "extra_var" is the number of these elements.
3286 * "dim" is the offset in the variables of "tab" where we should
3287 * start considering the elements in "list".
3288 * When this function returns, the total number of variables in "tab"
3289 * is equal to "dim" plus the number of elements in "list".
3291 * The newly added existentially quantified variables are not given
3292 * an explicit representation because the corresponding div constraints
3293 * do not appear in info->bmap. These constraints are not added
3294 * to info->bmap because for internal consistency, they would need to
3295 * be added to info->tab as well, where they could combine with the equality
3296 * that is added later to result in constraints that do not hold
3297 * in the original input.
3299 static isl_stat add_sub_vars(struct isl_coalesce_info *info,
3300 __isl_keep isl_aff_list *list, int dim, int extra_var)
3302 int i, j, n, d;
3303 isl_space *space;
3305 space = isl_basic_map_get_space(info->bmap);
3306 info->bmap = isl_basic_map_cow(info->bmap);
3307 info->bmap = isl_basic_map_extend_space(info->bmap, space,
3308 extra_var, 0, 0);
3309 if (!info->bmap)
3310 return isl_stat_error;
3311 n = isl_aff_list_n_aff(list);
3312 for (i = 0; i < n; ++i) {
3313 int is_nan;
3314 isl_aff *aff;
3316 aff = isl_aff_list_get_aff(list, i);
3317 is_nan = isl_aff_is_nan(aff);
3318 isl_aff_free(aff);
3319 if (is_nan < 0)
3320 return isl_stat_error;
3321 if (is_nan)
3322 continue;
3324 if (isl_tab_insert_var(info->tab, dim + i) < 0)
3325 return isl_stat_error;
3326 d = isl_basic_map_alloc_div(info->bmap);
3327 if (d < 0)
3328 return isl_stat_error;
3329 info->bmap = isl_basic_map_mark_div_unknown(info->bmap, d);
3330 if (!info->bmap)
3331 return isl_stat_error;
3332 for (j = d; j > i; --j)
3333 isl_basic_map_swap_div(info->bmap, j - 1, j);
3336 return isl_stat_ok;
3339 /* For each element in "list" that is not set to NaN, fix the corresponding
3340 * variable in "tab" to the purely affine expression defined by the element.
3341 * "dim" is the offset in the variables of "tab" where we should
3342 * start considering the elements in "list".
3344 * This function assumes that a sufficient number of rows and
3345 * elements in the constraint array are available in the tableau.
3347 static int add_sub_equalities(struct isl_tab *tab,
3348 __isl_keep isl_aff_list *list, int dim)
3350 int i, n;
3351 isl_ctx *ctx;
3352 isl_vec *sub;
3353 isl_aff *aff;
3355 n = isl_aff_list_n_aff(list);
3357 ctx = isl_tab_get_ctx(tab);
3358 sub = isl_vec_alloc(ctx, 1 + dim + n);
3359 if (!sub)
3360 return -1;
3361 isl_seq_clr(sub->el + 1 + dim, n);
3363 for (i = 0; i < n; ++i) {
3364 aff = isl_aff_list_get_aff(list, i);
3365 if (!aff)
3366 goto error;
3367 if (isl_aff_is_nan(aff)) {
3368 isl_aff_free(aff);
3369 continue;
3371 isl_seq_cpy(sub->el, aff->v->el + 1, 1 + dim);
3372 isl_int_neg(sub->el[1 + dim + i], aff->v->el[0]);
3373 if (isl_tab_add_eq(tab, sub->el) < 0)
3374 goto error;
3375 isl_int_set_si(sub->el[1 + dim + i], 0);
3376 isl_aff_free(aff);
3379 isl_vec_free(sub);
3380 return 0;
3381 error:
3382 isl_aff_free(aff);
3383 isl_vec_free(sub);
3384 return -1;
3387 /* Add variables to info->tab and info->bmap corresponding to the elements
3388 * in "list" that are not set to NaN. The value of the added variable
3389 * in info->tab is fixed to the purely affine expression defined by the element.
3390 * "dim" is the offset in the variables of info->tab where we should
3391 * start considering the elements in "list".
3392 * When this function returns, the total number of variables in info->tab
3393 * is equal to "dim" plus the number of elements in "list".
3395 static int add_subs(struct isl_coalesce_info *info,
3396 __isl_keep isl_aff_list *list, int dim)
3398 int extra_var;
3399 int n;
3401 if (!list)
3402 return -1;
3404 n = isl_aff_list_n_aff(list);
3405 extra_var = n - (info->tab->n_var - dim);
3407 if (isl_tab_extend_vars(info->tab, extra_var) < 0)
3408 return -1;
3409 if (isl_tab_extend_cons(info->tab, 2 * extra_var) < 0)
3410 return -1;
3411 if (add_sub_vars(info, list, dim, extra_var) < 0)
3412 return -1;
3414 return add_sub_equalities(info->tab, list, dim);
3417 /* Coalesce basic map "j" into basic map "i" after adding the extra integer
3418 * divisions in "i" but not in "j" to basic map "j", with values
3419 * specified by "list". The total number of elements in "list"
3420 * is equal to the number of integer divisions in "i", while the number
3421 * of NaN elements in the list is equal to the number of integer divisions
3422 * in "j".
3424 * If no coalescing can be performed, then we need to revert basic map "j"
3425 * to its original state. We do the same if basic map "i" gets dropped
3426 * during the coalescing, even though this should not happen in practice
3427 * since we have already checked for "j" being a subset of "i"
3428 * before we reach this stage.
3430 static enum isl_change coalesce_with_subs(int i, int j,
3431 struct isl_coalesce_info *info, __isl_keep isl_aff_list *list)
3433 isl_basic_map *bmap_j;
3434 struct isl_tab_undo *snap;
3435 unsigned dim;
3436 enum isl_change change;
3438 bmap_j = isl_basic_map_copy(info[j].bmap);
3439 snap = isl_tab_snap(info[j].tab);
3441 dim = isl_basic_map_dim(bmap_j, isl_dim_all);
3442 dim -= isl_basic_map_dim(bmap_j, isl_dim_div);
3443 if (add_subs(&info[j], list, dim) < 0)
3444 goto error;
3446 change = coalesce_local_pair(i, j, info);
3447 if (change != isl_change_none && change != isl_change_drop_first) {
3448 isl_basic_map_free(bmap_j);
3449 } else {
3450 isl_basic_map_free(info[j].bmap);
3451 info[j].bmap = bmap_j;
3453 if (isl_tab_rollback(info[j].tab, snap) < 0)
3454 return isl_change_error;
3457 return change;
3458 error:
3459 isl_basic_map_free(bmap_j);
3460 return isl_change_error;
3463 /* Check if we can coalesce basic map "j" into basic map "i" after copying
3464 * those extra integer divisions in "i" that can be simplified away
3465 * using the extra equalities in "j".
3466 * All divs are assumed to be known and not contain any nested divs.
3468 * We first check if there are any extra equalities in "j" that we
3469 * can exploit. Then we check if every integer division in "i"
3470 * either already appears in "j" or can be simplified using the
3471 * extra equalities to a purely affine expression.
3472 * If these tests succeed, then we try to coalesce the two basic maps
3473 * by introducing extra dimensions in "j" corresponding to
3474 * the extra integer divsisions "i" fixed to the corresponding
3475 * purely affine expression.
3477 static enum isl_change check_coalesce_into_eq(int i, int j,
3478 struct isl_coalesce_info *info)
3480 unsigned n_div_i, n_div_j;
3481 isl_basic_map *hull_i, *hull_j;
3482 int equal, empty;
3483 isl_aff_list *list;
3484 enum isl_change change;
3486 n_div_i = isl_basic_map_dim(info[i].bmap, isl_dim_div);
3487 n_div_j = isl_basic_map_dim(info[j].bmap, isl_dim_div);
3488 if (n_div_i <= n_div_j)
3489 return isl_change_none;
3490 if (info[j].bmap->n_eq == 0)
3491 return isl_change_none;
3493 hull_i = isl_basic_map_copy(info[i].bmap);
3494 hull_i = isl_basic_map_plain_affine_hull(hull_i);
3495 hull_j = isl_basic_map_copy(info[j].bmap);
3496 hull_j = isl_basic_map_plain_affine_hull(hull_j);
3498 hull_j = isl_basic_map_intersect(hull_j, isl_basic_map_copy(hull_i));
3499 equal = isl_basic_map_plain_is_equal(hull_i, hull_j);
3500 empty = isl_basic_map_plain_is_empty(hull_j);
3501 isl_basic_map_free(hull_i);
3503 if (equal < 0 || empty < 0)
3504 goto error;
3505 if (equal || empty) {
3506 isl_basic_map_free(hull_j);
3507 return isl_change_none;
3510 list = set_up_substitutions(info[i].bmap, info[j].bmap, hull_j);
3511 if (!list)
3512 return isl_change_error;
3513 if (isl_aff_list_n_aff(list) < n_div_i)
3514 change = isl_change_none;
3515 else
3516 change = coalesce_with_subs(i, j, info, list);
3518 isl_aff_list_free(list);
3520 return change;
3521 error:
3522 isl_basic_map_free(hull_j);
3523 return isl_change_error;
3526 /* Check if we can coalesce basic maps "i" and "j" after copying
3527 * those extra integer divisions in one of the basic maps that can
3528 * be simplified away using the extra equalities in the other basic map.
3529 * We require all divs to be known in both basic maps.
3530 * Furthermore, to simplify the comparison of div expressions,
3531 * we do not allow any nested integer divisions.
3533 static enum isl_change check_coalesce_eq(int i, int j,
3534 struct isl_coalesce_info *info)
3536 isl_bool known, nested;
3537 enum isl_change change;
3539 known = isl_basic_map_divs_known(info[i].bmap);
3540 if (known < 0 || !known)
3541 return known < 0 ? isl_change_error : isl_change_none;
3542 known = isl_basic_map_divs_known(info[j].bmap);
3543 if (known < 0 || !known)
3544 return known < 0 ? isl_change_error : isl_change_none;
3545 nested = has_nested_div(info[i].bmap);
3546 if (nested < 0 || nested)
3547 return nested < 0 ? isl_change_error : isl_change_none;
3548 nested = has_nested_div(info[j].bmap);
3549 if (nested < 0 || nested)
3550 return nested < 0 ? isl_change_error : isl_change_none;
3552 change = check_coalesce_into_eq(i, j, info);
3553 if (change != isl_change_none)
3554 return change;
3555 change = check_coalesce_into_eq(j, i, info);
3556 if (change != isl_change_none)
3557 return invert_change(change);
3559 return isl_change_none;
3562 /* Check if the union of the given pair of basic maps
3563 * can be represented by a single basic map.
3564 * If so, replace the pair by the single basic map and return
3565 * isl_change_drop_first, isl_change_drop_second or isl_change_fuse.
3566 * Otherwise, return isl_change_none.
3568 * We first check if the two basic maps live in the same local space,
3569 * after aligning the divs that differ by only an integer constant.
3570 * If so, we do the complete check. Otherwise, we check if they have
3571 * the same number of integer divisions and can be coalesced, if one is
3572 * an obvious subset of the other or if the extra integer divisions
3573 * of one basic map can be simplified away using the extra equalities
3574 * of the other basic map.
3576 static enum isl_change coalesce_pair(int i, int j,
3577 struct isl_coalesce_info *info)
3579 isl_bool same;
3580 enum isl_change change;
3582 if (harmonize_divs(&info[i], &info[j]) < 0)
3583 return isl_change_error;
3584 same = same_divs(info[i].bmap, info[j].bmap);
3585 if (same < 0)
3586 return isl_change_error;
3587 if (same)
3588 return coalesce_local_pair(i, j, info);
3590 if (info[i].bmap->n_div == info[j].bmap->n_div) {
3591 change = coalesce_local_pair(i, j, info);
3592 if (change != isl_change_none)
3593 return change;
3596 change = coalesce_divs(i, j, info);
3597 if (change != isl_change_none)
3598 return change;
3600 return check_coalesce_eq(i, j, info);
3603 /* Return the maximum of "a" and "b".
3605 static int isl_max(int a, int b)
3607 return a > b ? a : b;
3610 /* Pairwise coalesce the basic maps in the range [start1, end1[ of "info"
3611 * with those in the range [start2, end2[, skipping basic maps
3612 * that have been removed (either before or within this function).
3614 * For each basic map i in the first range, we check if it can be coalesced
3615 * with respect to any previously considered basic map j in the second range.
3616 * If i gets dropped (because it was a subset of some j), then
3617 * we can move on to the next basic map.
3618 * If j gets dropped, we need to continue checking against the other
3619 * previously considered basic maps.
3620 * If the two basic maps got fused, then we recheck the fused basic map
3621 * against the previously considered basic maps, starting at i + 1
3622 * (even if start2 is greater than i + 1).
3624 static int coalesce_range(isl_ctx *ctx, struct isl_coalesce_info *info,
3625 int start1, int end1, int start2, int end2)
3627 int i, j;
3629 for (i = end1 - 1; i >= start1; --i) {
3630 if (info[i].removed)
3631 continue;
3632 for (j = isl_max(i + 1, start2); j < end2; ++j) {
3633 enum isl_change changed;
3635 if (info[j].removed)
3636 continue;
3637 if (info[i].removed)
3638 isl_die(ctx, isl_error_internal,
3639 "basic map unexpectedly removed",
3640 return -1);
3641 changed = coalesce_pair(i, j, info);
3642 switch (changed) {
3643 case isl_change_error:
3644 return -1;
3645 case isl_change_none:
3646 case isl_change_drop_second:
3647 continue;
3648 case isl_change_drop_first:
3649 j = end2;
3650 break;
3651 case isl_change_fuse:
3652 j = i;
3653 break;
3658 return 0;
3661 /* Pairwise coalesce the basic maps described by the "n" elements of "info".
3663 * We consider groups of basic maps that live in the same apparent
3664 * affine hull and we first coalesce within such a group before we
3665 * coalesce the elements in the group with elements of previously
3666 * considered groups. If a fuse happens during the second phase,
3667 * then we also reconsider the elements within the group.
3669 static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
3671 int start, end;
3673 for (end = n; end > 0; end = start) {
3674 start = end - 1;
3675 while (start >= 1 &&
3676 info[start - 1].hull_hash == info[start].hull_hash)
3677 start--;
3678 if (coalesce_range(ctx, info, start, end, start, end) < 0)
3679 return -1;
3680 if (coalesce_range(ctx, info, start, end, end, n) < 0)
3681 return -1;
3684 return 0;
3687 /* Update the basic maps in "map" based on the information in "info".
3688 * In particular, remove the basic maps that have been marked removed and
3689 * update the others based on the information in the corresponding tableau.
3690 * Since we detected implicit equalities without calling
3691 * isl_basic_map_gauss, we need to do it now.
3692 * Also call isl_basic_map_simplify if we may have lost the definition
3693 * of one or more integer divisions.
3695 static __isl_give isl_map *update_basic_maps(__isl_take isl_map *map,
3696 int n, struct isl_coalesce_info *info)
3698 int i;
3700 if (!map)
3701 return NULL;
3703 for (i = n - 1; i >= 0; --i) {
3704 if (info[i].removed) {
3705 isl_basic_map_free(map->p[i]);
3706 if (i != map->n - 1)
3707 map->p[i] = map->p[map->n - 1];
3708 map->n--;
3709 continue;
3712 info[i].bmap = isl_basic_map_update_from_tab(info[i].bmap,
3713 info[i].tab);
3714 info[i].bmap = isl_basic_map_gauss(info[i].bmap, NULL);
3715 if (info[i].simplify)
3716 info[i].bmap = isl_basic_map_simplify(info[i].bmap);
3717 info[i].bmap = isl_basic_map_finalize(info[i].bmap);
3718 if (!info[i].bmap)
3719 return isl_map_free(map);
3720 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT);
3721 ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3722 isl_basic_map_free(map->p[i]);
3723 map->p[i] = info[i].bmap;
3724 info[i].bmap = NULL;
3727 return map;
3730 /* For each pair of basic maps in the map, check if the union of the two
3731 * can be represented by a single basic map.
3732 * If so, replace the pair by the single basic map and start over.
3734 * We factor out any (hidden) common factor from the constraint
3735 * coefficients to improve the detection of adjacent constraints.
3737 * Since we are constructing the tableaus of the basic maps anyway,
3738 * we exploit them to detect implicit equalities and redundant constraints.
3739 * This also helps the coalescing as it can ignore the redundant constraints.
3740 * In order to avoid confusion, we make all implicit equalities explicit
3741 * in the basic maps. We don't call isl_basic_map_gauss, though,
3742 * as that may affect the number of constraints.
3743 * This means that we have to call isl_basic_map_gauss at the end
3744 * of the computation (in update_basic_maps) to ensure that
3745 * the basic maps are not left in an unexpected state.
3746 * For each basic map, we also compute the hash of the apparent affine hull
3747 * for use in coalesce.
3749 __isl_give isl_map *isl_map_coalesce(__isl_take isl_map *map)
3751 int i;
3752 unsigned n;
3753 isl_ctx *ctx;
3754 struct isl_coalesce_info *info = NULL;
3756 map = isl_map_remove_empty_parts(map);
3757 if (!map)
3758 return NULL;
3760 if (map->n <= 1)
3761 return map;
3763 ctx = isl_map_get_ctx(map);
3764 map = isl_map_sort_divs(map);
3765 map = isl_map_cow(map);
3767 if (!map)
3768 return NULL;
3770 n = map->n;
3772 info = isl_calloc_array(map->ctx, struct isl_coalesce_info, n);
3773 if (!info)
3774 goto error;
3776 for (i = 0; i < map->n; ++i) {
3777 map->p[i] = isl_basic_map_reduce_coefficients(map->p[i]);
3778 if (!map->p[i])
3779 goto error;
3780 info[i].bmap = isl_basic_map_copy(map->p[i]);
3781 info[i].tab = isl_tab_from_basic_map(info[i].bmap, 0);
3782 if (!info[i].tab)
3783 goto error;
3784 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_IMPLICIT))
3785 if (isl_tab_detect_implicit_equalities(info[i].tab) < 0)
3786 goto error;
3787 info[i].bmap = isl_tab_make_equalities_explicit(info[i].tab,
3788 info[i].bmap);
3789 if (!info[i].bmap)
3790 goto error;
3791 if (!ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_NO_REDUNDANT))
3792 if (isl_tab_detect_redundant(info[i].tab) < 0)
3793 goto error;
3794 if (coalesce_info_set_hull_hash(&info[i]) < 0)
3795 goto error;
3797 for (i = map->n - 1; i >= 0; --i)
3798 if (info[i].tab->empty)
3799 drop(&info[i]);
3801 if (coalesce(ctx, n, info) < 0)
3802 goto error;
3804 map = update_basic_maps(map, n, info);
3806 clear_coalesce_info(n, info);
3808 return map;
3809 error:
3810 clear_coalesce_info(n, info);
3811 isl_map_free(map);
3812 return NULL;
3815 /* For each pair of basic sets in the set, check if the union of the two
3816 * can be represented by a single basic set.
3817 * If so, replace the pair by the single basic set and start over.
3819 struct isl_set *isl_set_coalesce(struct isl_set *set)
3821 return set_from_map(isl_map_coalesce(set_to_map(set)));