isl_tab_pip.c: sol_{map,pma}_add_empty: drop spurious error message
[isl.git] / isl_coalesce.c
blob919282bfb9069afa161c5fb22507b70447c453ec
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include "isl_map_private.h"
16 #include <isl_seq.h>
17 #include <isl/options.h>
18 #include "isl_tab.h"
19 #include <isl_mat_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
23 #define STATUS_ERROR -1
24 #define STATUS_REDUNDANT 1
25 #define STATUS_VALID 2
26 #define STATUS_SEPARATE 3
27 #define STATUS_CUT 4
28 #define STATUS_ADJ_EQ 5
29 #define STATUS_ADJ_INEQ 6
31 static int status_in(isl_int *ineq, struct isl_tab *tab)
33 enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
34 switch (type) {
35 default:
36 case isl_ineq_error: return STATUS_ERROR;
37 case isl_ineq_redundant: return STATUS_VALID;
38 case isl_ineq_separate: return STATUS_SEPARATE;
39 case isl_ineq_cut: return STATUS_CUT;
40 case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
41 case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
45 /* Compute the position of the equalities of basic map "bmap_i"
46 * with respect to the basic map represented by "tab_j".
47 * The resulting array has twice as many entries as the number
48 * of equalities corresponding to the two inequalties to which
49 * each equality corresponds.
51 static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
52 struct isl_tab *tab_j)
54 int k, l;
55 int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
56 unsigned dim;
58 if (!eq)
59 return NULL;
61 dim = isl_basic_map_total_dim(bmap_i);
62 for (k = 0; k < bmap_i->n_eq; ++k) {
63 for (l = 0; l < 2; ++l) {
64 isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
65 eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
66 if (eq[2 * k + l] == STATUS_ERROR)
67 goto error;
69 if (eq[2 * k] == STATUS_SEPARATE ||
70 eq[2 * k + 1] == STATUS_SEPARATE)
71 break;
74 return eq;
75 error:
76 free(eq);
77 return NULL;
80 /* Compute the position of the inequalities of basic map "bmap_i"
81 * (also represented by "tab_i", if not NULL) with respect to the basic map
82 * represented by "tab_j".
84 static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
85 struct isl_tab *tab_i, struct isl_tab *tab_j)
87 int k;
88 unsigned n_eq = bmap_i->n_eq;
89 int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
91 if (!ineq)
92 return NULL;
94 for (k = 0; k < bmap_i->n_ineq; ++k) {
95 if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
96 ineq[k] = STATUS_REDUNDANT;
97 continue;
99 ineq[k] = status_in(bmap_i->ineq[k], tab_j);
100 if (ineq[k] == STATUS_ERROR)
101 goto error;
102 if (ineq[k] == STATUS_SEPARATE)
103 break;
106 return ineq;
107 error:
108 free(ineq);
109 return NULL;
112 static int any(int *con, unsigned len, int status)
114 int i;
116 for (i = 0; i < len ; ++i)
117 if (con[i] == status)
118 return 1;
119 return 0;
122 static int count(int *con, unsigned len, int status)
124 int i;
125 int c = 0;
127 for (i = 0; i < len ; ++i)
128 if (con[i] == status)
129 c++;
130 return c;
133 static int all(int *con, unsigned len, int status)
135 int i;
137 for (i = 0; i < len ; ++i) {
138 if (con[i] == STATUS_REDUNDANT)
139 continue;
140 if (con[i] != status)
141 return 0;
143 return 1;
146 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
148 isl_basic_map_free(map->p[i]);
149 isl_tab_free(tabs[i]);
151 if (i != map->n - 1) {
152 map->p[i] = map->p[map->n - 1];
153 tabs[i] = tabs[map->n - 1];
155 tabs[map->n - 1] = NULL;
156 map->n--;
159 /* Replace the pair of basic maps i and j by the basic map bounded
160 * by the valid constraints in both basic maps and the constraint
161 * in extra (if not NULL).
163 static int fuse(struct isl_map *map, int i, int j,
164 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
165 __isl_keep isl_mat *extra)
167 int k, l;
168 struct isl_basic_map *fused = NULL;
169 struct isl_tab *fused_tab = NULL;
170 unsigned total = isl_basic_map_total_dim(map->p[i]);
171 unsigned extra_rows = extra ? extra->n_row : 0;
173 fused = isl_basic_map_alloc_space(isl_space_copy(map->p[i]->dim),
174 map->p[i]->n_div,
175 map->p[i]->n_eq + map->p[j]->n_eq,
176 map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
177 if (!fused)
178 goto error;
180 for (k = 0; k < map->p[i]->n_eq; ++k) {
181 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
182 eq_i[2 * k + 1] != STATUS_VALID))
183 continue;
184 l = isl_basic_map_alloc_equality(fused);
185 if (l < 0)
186 goto error;
187 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
190 for (k = 0; k < map->p[j]->n_eq; ++k) {
191 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
192 eq_j[2 * k + 1] != STATUS_VALID))
193 continue;
194 l = isl_basic_map_alloc_equality(fused);
195 if (l < 0)
196 goto error;
197 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
200 for (k = 0; k < map->p[i]->n_ineq; ++k) {
201 if (ineq_i[k] != STATUS_VALID)
202 continue;
203 l = isl_basic_map_alloc_inequality(fused);
204 if (l < 0)
205 goto error;
206 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
209 for (k = 0; k < map->p[j]->n_ineq; ++k) {
210 if (ineq_j[k] != STATUS_VALID)
211 continue;
212 l = isl_basic_map_alloc_inequality(fused);
213 if (l < 0)
214 goto error;
215 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
218 for (k = 0; k < map->p[i]->n_div; ++k) {
219 int l = isl_basic_map_alloc_div(fused);
220 if (l < 0)
221 goto error;
222 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
225 for (k = 0; k < extra_rows; ++k) {
226 l = isl_basic_map_alloc_inequality(fused);
227 if (l < 0)
228 goto error;
229 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
232 fused = isl_basic_map_gauss(fused, NULL);
233 ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
234 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
235 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
236 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
238 fused_tab = isl_tab_from_basic_map(fused, 0);
239 if (isl_tab_detect_redundant(fused_tab) < 0)
240 goto error;
242 isl_basic_map_free(map->p[i]);
243 map->p[i] = fused;
244 isl_tab_free(tabs[i]);
245 tabs[i] = fused_tab;
246 drop(map, j, tabs);
248 return 1;
249 error:
250 isl_tab_free(fused_tab);
251 isl_basic_map_free(fused);
252 return -1;
255 /* Given a pair of basic maps i and j such that all constraints are either
256 * "valid" or "cut", check if the facets corresponding to the "cut"
257 * constraints of i lie entirely within basic map j.
258 * If so, replace the pair by the basic map consisting of the valid
259 * constraints in both basic maps.
261 * To see that we are not introducing any extra points, call the
262 * two basic maps A and B and the resulting map U and let x
263 * be an element of U \setminus ( A \cup B ).
264 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
265 * violates them. Let X be the intersection of U with the opposites
266 * of these constraints. Then x \in X.
267 * The facet corresponding to c_1 contains the corresponding facet of A.
268 * This facet is entirely contained in B, so c_2 is valid on the facet.
269 * However, since it is also (part of) a facet of X, -c_2 is also valid
270 * on the facet. This means c_2 is saturated on the facet, so c_1 and
271 * c_2 must be opposites of each other, but then x could not violate
272 * both of them.
274 static int check_facets(struct isl_map *map, int i, int j,
275 struct isl_tab **tabs, int *ineq_i, int *ineq_j)
277 int k, l;
278 struct isl_tab_undo *snap;
279 unsigned n_eq = map->p[i]->n_eq;
281 snap = isl_tab_snap(tabs[i]);
283 for (k = 0; k < map->p[i]->n_ineq; ++k) {
284 if (ineq_i[k] != STATUS_CUT)
285 continue;
286 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
287 return -1;
288 for (l = 0; l < map->p[j]->n_ineq; ++l) {
289 int stat;
290 if (ineq_j[l] != STATUS_CUT)
291 continue;
292 stat = status_in(map->p[j]->ineq[l], tabs[i]);
293 if (stat < 0)
294 return -1;
295 if (stat != STATUS_VALID)
296 break;
298 if (isl_tab_rollback(tabs[i], snap) < 0)
299 return -1;
300 if (l < map->p[j]->n_ineq)
301 break;
304 if (k < map->p[i]->n_ineq)
305 /* BAD CUT PAIR */
306 return 0;
307 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
310 /* Check if basic map "i" contains the basic map represented
311 * by the tableau "tab".
313 static int contains(struct isl_map *map, int i, int *ineq_i,
314 struct isl_tab *tab)
316 int k, l;
317 unsigned dim;
319 dim = isl_basic_map_total_dim(map->p[i]);
320 for (k = 0; k < map->p[i]->n_eq; ++k) {
321 for (l = 0; l < 2; ++l) {
322 int stat;
323 isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
324 stat = status_in(map->p[i]->eq[k], tab);
325 if (stat < 0)
326 return -1;
327 if (stat != STATUS_VALID)
328 return 0;
332 for (k = 0; k < map->p[i]->n_ineq; ++k) {
333 int stat;
334 if (ineq_i[k] == STATUS_REDUNDANT)
335 continue;
336 stat = status_in(map->p[i]->ineq[k], tab);
337 if (stat < 0)
338 return -1;
339 if (stat != STATUS_VALID)
340 return 0;
342 return 1;
345 /* Basic map "i" has an inequality (say "k") that is adjacent
346 * to some inequality of basic map "j". All the other inequalities
347 * are valid for "j".
348 * Check if basic map "j" forms an extension of basic map "i".
350 * Note that this function is only called if some of the equalities or
351 * inequalities of basic map "j" do cut basic map "i". The function is
352 * correct even if there are no such cut constraints, but in that case
353 * the additional checks performed by this function are overkill.
355 * In particular, we replace constraint k, say f >= 0, by constraint
356 * f <= -1, add the inequalities of "j" that are valid for "i"
357 * and check if the result is a subset of basic map "j".
358 * If so, then we know that this result is exactly equal to basic map "j"
359 * since all its constraints are valid for basic map "j".
360 * By combining the valid constraints of "i" (all equalities and all
361 * inequalities except "k") and the valid constraints of "j" we therefore
362 * obtain a basic map that is equal to their union.
363 * In this case, there is no need to perform a rollback of the tableau
364 * since it is going to be destroyed in fuse().
367 * |\__ |\__
368 * | \__ | \__
369 * | \_ => | \__
370 * |_______| _ |_________\
373 * |\ |\
374 * | \ | \
375 * | \ | \
376 * | | | \
377 * | ||\ => | \
378 * | || \ | \
379 * | || | | |
380 * |__||_/ |_____/
382 static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
383 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
385 int k;
386 struct isl_tab_undo *snap;
387 unsigned n_eq = map->p[i]->n_eq;
388 unsigned total = isl_basic_map_total_dim(map->p[i]);
389 int r;
390 int super;
392 if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
393 return -1;
395 for (k = 0; k < map->p[i]->n_ineq; ++k)
396 if (ineq_i[k] == STATUS_ADJ_INEQ)
397 break;
398 if (k >= map->p[i]->n_ineq)
399 isl_die(isl_map_get_ctx(map), isl_error_internal,
400 "ineq_i should have exactly one STATUS_ADJ_INEQ",
401 return -1);
403 snap = isl_tab_snap(tabs[i]);
405 if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
406 return -1;
408 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
409 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
410 r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
411 isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
412 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
413 if (r < 0)
414 return -1;
416 for (k = 0; k < map->p[j]->n_ineq; ++k) {
417 if (ineq_j[k] != STATUS_VALID)
418 continue;
419 if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
420 return -1;
423 super = contains(map, j, ineq_j, tabs[i]);
424 if (super < 0)
425 return -1;
426 if (super)
427 return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
429 if (isl_tab_rollback(tabs[i], snap) < 0)
430 return -1;
432 return 0;
436 /* Both basic maps have at least one inequality with and adjacent
437 * (but opposite) inequality in the other basic map.
438 * Check that there are no cut constraints and that there is only
439 * a single pair of adjacent inequalities.
440 * If so, we can replace the pair by a single basic map described
441 * by all but the pair of adjacent inequalities.
442 * Any additional points introduced lie strictly between the two
443 * adjacent hyperplanes and can therefore be integral.
445 * ____ _____
446 * / ||\ / \
447 * / || \ / \
448 * \ || \ => \ \
449 * \ || / \ /
450 * \___||_/ \_____/
452 * The test for a single pair of adjancent inequalities is important
453 * for avoiding the combination of two basic maps like the following
455 * /|
456 * / |
457 * /__|
458 * _____
459 * | |
460 * | |
461 * |___|
463 * If there are some cut constraints on one side, then we may
464 * still be able to fuse the two basic maps, but we need to perform
465 * some additional checks in is_adj_ineq_extension.
467 static int check_adj_ineq(struct isl_map *map, int i, int j,
468 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
470 int count_i, count_j;
471 int cut_i, cut_j;
473 count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
474 count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
476 if (count_i != 1 && count_j != 1)
477 return 0;
479 cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
480 any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
481 cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
482 any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
484 if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
485 return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
487 if (count_i == 1 && !cut_i)
488 return is_adj_ineq_extension(map, i, j, tabs,
489 eq_i, ineq_i, eq_j, ineq_j);
491 if (count_j == 1 && !cut_j)
492 return is_adj_ineq_extension(map, j, i, tabs,
493 eq_j, ineq_j, eq_i, ineq_i);
495 return 0;
498 /* Basic map "i" has an inequality "k" that is adjacent to some equality
499 * of basic map "j". All the other inequalities are valid for "j".
500 * Check if basic map "j" forms an extension of basic map "i".
502 * In particular, we relax constraint "k", compute the corresponding
503 * facet and check whether it is included in the other basic map.
504 * If so, we know that relaxing the constraint extends the basic
505 * map with exactly the other basic map (we already know that this
506 * other basic map is included in the extension, because there
507 * were no "cut" inequalities in "i") and we can replace the
508 * two basic maps by this extension.
509 * ____ _____
510 * / || / |
511 * / || / |
512 * \ || => \ |
513 * \ || \ |
514 * \___|| \____|
516 static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
517 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
519 int changed = 0;
520 int super;
521 struct isl_tab_undo *snap, *snap2;
522 unsigned n_eq = map->p[i]->n_eq;
524 if (isl_tab_is_equality(tabs[i], n_eq + k))
525 return 0;
527 snap = isl_tab_snap(tabs[i]);
528 tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
529 snap2 = isl_tab_snap(tabs[i]);
530 if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
531 return -1;
532 super = contains(map, j, ineq_j, tabs[i]);
533 if (super < 0)
534 return -1;
535 if (super) {
536 if (isl_tab_rollback(tabs[i], snap2) < 0)
537 return -1;
538 map->p[i] = isl_basic_map_cow(map->p[i]);
539 if (!map->p[i])
540 return -1;
541 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
542 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
543 drop(map, j, tabs);
544 changed = 1;
545 } else
546 if (isl_tab_rollback(tabs[i], snap) < 0)
547 return -1;
549 return changed;
552 /* Data structure that keeps track of the wrapping constraints
553 * and of information to bound the coefficients of those constraints.
555 * bound is set if we want to apply a bound on the coefficients
556 * mat contains the wrapping constraints
557 * max is the bound on the coefficients (if bound is set)
559 struct isl_wraps {
560 int bound;
561 isl_mat *mat;
562 isl_int max;
565 /* Update wraps->max to be greater than or equal to the coefficients
566 * in the equalities and inequalities of bmap that can be removed if we end up
567 * applying wrapping.
569 static void wraps_update_max(struct isl_wraps *wraps,
570 __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
572 int k;
573 isl_int max_k;
574 unsigned total = isl_basic_map_total_dim(bmap);
576 isl_int_init(max_k);
578 for (k = 0; k < bmap->n_eq; ++k) {
579 if (eq[2 * k] == STATUS_VALID &&
580 eq[2 * k + 1] == STATUS_VALID)
581 continue;
582 isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
583 if (isl_int_abs_gt(max_k, wraps->max))
584 isl_int_set(wraps->max, max_k);
587 for (k = 0; k < bmap->n_ineq; ++k) {
588 if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
589 continue;
590 isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
591 if (isl_int_abs_gt(max_k, wraps->max))
592 isl_int_set(wraps->max, max_k);
595 isl_int_clear(max_k);
598 /* Initialize the isl_wraps data structure.
599 * If we want to bound the coefficients of the wrapping constraints,
600 * we set wraps->max to the largest coefficient
601 * in the equalities and inequalities that can be removed if we end up
602 * applying wrapping.
604 static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
605 __isl_keep isl_map *map, int i, int j,
606 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
608 isl_ctx *ctx;
610 wraps->bound = 0;
611 wraps->mat = mat;
612 if (!mat)
613 return;
614 ctx = isl_mat_get_ctx(mat);
615 wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
616 if (!wraps->bound)
617 return;
618 isl_int_init(wraps->max);
619 isl_int_set_si(wraps->max, 0);
620 wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
621 wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
624 /* Free the contents of the isl_wraps data structure.
626 static void wraps_free(struct isl_wraps *wraps)
628 isl_mat_free(wraps->mat);
629 if (wraps->bound)
630 isl_int_clear(wraps->max);
633 /* Is the wrapping constraint in row "row" allowed?
635 * If wraps->bound is set, we check that none of the coefficients
636 * is greater than wraps->max.
638 static int allow_wrap(struct isl_wraps *wraps, int row)
640 int i;
642 if (!wraps->bound)
643 return 1;
645 for (i = 1; i < wraps->mat->n_col; ++i)
646 if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
647 return 0;
649 return 1;
652 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
653 * wrap the constraint around "bound" such that it includes the whole
654 * set "set" and append the resulting constraint to "wraps".
655 * "wraps" is assumed to have been pre-allocated to the appropriate size.
656 * wraps->n_row is the number of actual wrapped constraints that have
657 * been added.
658 * If any of the wrapping problems results in a constraint that is
659 * identical to "bound", then this means that "set" is unbounded in such
660 * way that no wrapping is possible. If this happens then wraps->n_row
661 * is reset to zero.
662 * Similarly, if we want to bound the coefficients of the wrapping
663 * constraints and a newly added wrapping constraint does not
664 * satisfy the bound, then wraps->n_row is also reset to zero.
666 static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
667 struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
669 int l;
670 int w;
671 unsigned total = isl_basic_map_total_dim(bmap);
673 w = wraps->mat->n_row;
675 for (l = 0; l < bmap->n_ineq; ++l) {
676 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
677 continue;
678 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
679 continue;
680 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
681 continue;
683 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
684 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
685 return -1;
686 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
687 goto unbounded;
688 if (!allow_wrap(wraps, w))
689 goto unbounded;
690 ++w;
692 for (l = 0; l < bmap->n_eq; ++l) {
693 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
694 continue;
695 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
696 continue;
698 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
699 isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
700 if (!isl_set_wrap_facet(set, wraps->mat->row[w],
701 wraps->mat->row[w + 1]))
702 return -1;
703 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
704 goto unbounded;
705 if (!allow_wrap(wraps, w))
706 goto unbounded;
707 ++w;
709 isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
710 if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
711 return -1;
712 if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
713 goto unbounded;
714 if (!allow_wrap(wraps, w))
715 goto unbounded;
716 ++w;
719 wraps->mat->n_row = w;
720 return 0;
721 unbounded:
722 wraps->mat->n_row = 0;
723 return 0;
726 /* Check if the constraints in "wraps" from "first" until the last
727 * are all valid for the basic set represented by "tab".
728 * If not, wraps->n_row is set to zero.
730 static int check_wraps(__isl_keep isl_mat *wraps, int first,
731 struct isl_tab *tab)
733 int i;
735 for (i = first; i < wraps->n_row; ++i) {
736 enum isl_ineq_type type;
737 type = isl_tab_ineq_type(tab, wraps->row[i]);
738 if (type == isl_ineq_error)
739 return -1;
740 if (type == isl_ineq_redundant)
741 continue;
742 wraps->n_row = 0;
743 return 0;
746 return 0;
749 /* Return a set that corresponds to the non-redudant constraints
750 * (as recorded in tab) of bmap.
752 * It's important to remove the redundant constraints as some
753 * of the other constraints may have been modified after the
754 * constraints were marked redundant.
755 * In particular, a constraint may have been relaxed.
756 * Redundant constraints are ignored when a constraint is relaxed
757 * and should therefore continue to be ignored ever after.
758 * Otherwise, the relaxation might be thwarted by some of
759 * these constraints.
761 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
762 struct isl_tab *tab)
764 bmap = isl_basic_map_copy(bmap);
765 bmap = isl_basic_map_cow(bmap);
766 bmap = isl_basic_map_update_from_tab(bmap, tab);
767 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
770 /* Given a basic set i with a constraint k that is adjacent to either the
771 * whole of basic set j or a facet of basic set j, check if we can wrap
772 * both the facet corresponding to k and the facet of j (or the whole of j)
773 * around their ridges to include the other set.
774 * If so, replace the pair of basic sets by their union.
776 * All constraints of i (except k) are assumed to be valid for j.
778 * However, the constraints of j may not be valid for i and so
779 * we have to check that the wrapping constraints for j are valid for i.
781 * In the case where j has a facet adjacent to i, tab[j] is assumed
782 * to have been restricted to this facet, so that the non-redundant
783 * constraints in tab[j] are the ridges of the facet.
784 * Note that for the purpose of wrapping, it does not matter whether
785 * we wrap the ridges of i around the whole of j or just around
786 * the facet since all the other constraints are assumed to be valid for j.
787 * In practice, we wrap to include the whole of j.
788 * ____ _____
789 * / | / \
790 * / || / |
791 * \ || => \ |
792 * \ || \ |
793 * \___|| \____|
796 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
797 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
799 int changed = 0;
800 struct isl_wraps wraps;
801 isl_mat *mat;
802 struct isl_set *set_i = NULL;
803 struct isl_set *set_j = NULL;
804 struct isl_vec *bound = NULL;
805 unsigned total = isl_basic_map_total_dim(map->p[i]);
806 struct isl_tab_undo *snap;
807 int n;
809 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
810 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
811 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
812 map->p[i]->n_ineq + map->p[j]->n_ineq,
813 1 + total);
814 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
815 bound = isl_vec_alloc(map->ctx, 1 + total);
816 if (!set_i || !set_j || !wraps.mat || !bound)
817 goto error;
819 isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
820 isl_int_add_ui(bound->el[0], bound->el[0], 1);
822 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
823 wraps.mat->n_row = 1;
825 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
826 goto error;
827 if (!wraps.mat->n_row)
828 goto unbounded;
830 snap = isl_tab_snap(tabs[i]);
832 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
833 goto error;
834 if (isl_tab_detect_redundant(tabs[i]) < 0)
835 goto error;
837 isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
839 n = wraps.mat->n_row;
840 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
841 goto error;
843 if (isl_tab_rollback(tabs[i], snap) < 0)
844 goto error;
845 if (check_wraps(wraps.mat, n, tabs[i]) < 0)
846 goto error;
847 if (!wraps.mat->n_row)
848 goto unbounded;
850 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
852 unbounded:
853 wraps_free(&wraps);
855 isl_set_free(set_i);
856 isl_set_free(set_j);
858 isl_vec_free(bound);
860 return changed;
861 error:
862 wraps_free(&wraps);
863 isl_vec_free(bound);
864 isl_set_free(set_i);
865 isl_set_free(set_j);
866 return -1;
869 /* Set the is_redundant property of the "n" constraints in "cuts",
870 * except "k" to "v".
871 * This is a fairly tricky operation as it bypasses isl_tab.c.
872 * The reason we want to temporarily mark some constraints redundant
873 * is that we want to ignore them in add_wraps.
875 * Initially all cut constraints are non-redundant, but the
876 * selection of a facet right before the call to this function
877 * may have made some of them redundant.
878 * Likewise, the same constraints are marked non-redundant
879 * in the second call to this function, before they are officially
880 * made non-redundant again in the subsequent rollback.
882 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
883 int *cuts, int n, int k, int v)
885 int l;
887 for (l = 0; l < n; ++l) {
888 if (l == k)
889 continue;
890 tab->con[n_eq + cuts[l]].is_redundant = v;
894 /* Given a pair of basic maps i and j such that j sticks out
895 * of i at n cut constraints, each time by at most one,
896 * try to compute wrapping constraints and replace the two
897 * basic maps by a single basic map.
898 * The other constraints of i are assumed to be valid for j.
900 * The facets of i corresponding to the cut constraints are
901 * wrapped around their ridges, except those ridges determined
902 * by any of the other cut constraints.
903 * The intersections of cut constraints need to be ignored
904 * as the result of wrapping one cut constraint around another
905 * would result in a constraint cutting the union.
906 * In each case, the facets are wrapped to include the union
907 * of the two basic maps.
909 * The pieces of j that lie at an offset of exactly one from
910 * one of the cut constraints of i are wrapped around their edges.
911 * Here, there is no need to ignore intersections because we
912 * are wrapping around the union of the two basic maps.
914 * If any wrapping fails, i.e., if we cannot wrap to touch
915 * the union, then we give up.
916 * Otherwise, the pair of basic maps is replaced by their union.
918 static int wrap_in_facets(struct isl_map *map, int i, int j,
919 int *cuts, int n, struct isl_tab **tabs,
920 int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
922 int changed = 0;
923 struct isl_wraps wraps;
924 isl_mat *mat;
925 isl_set *set = NULL;
926 isl_vec *bound = NULL;
927 unsigned total = isl_basic_map_total_dim(map->p[i]);
928 int max_wrap;
929 int k;
930 struct isl_tab_undo *snap_i, *snap_j;
932 if (isl_tab_extend_cons(tabs[j], 1) < 0)
933 goto error;
935 max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
936 map->p[i]->n_ineq + map->p[j]->n_ineq;
937 max_wrap *= n;
939 set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
940 set_from_updated_bmap(map->p[j], tabs[j]));
941 mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
942 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
943 bound = isl_vec_alloc(map->ctx, 1 + total);
944 if (!set || !wraps.mat || !bound)
945 goto error;
947 snap_i = isl_tab_snap(tabs[i]);
948 snap_j = isl_tab_snap(tabs[j]);
950 wraps.mat->n_row = 0;
952 for (k = 0; k < n; ++k) {
953 if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
954 goto error;
955 if (isl_tab_detect_redundant(tabs[i]) < 0)
956 goto error;
957 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
959 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
960 if (!tabs[i]->empty &&
961 add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
962 goto error;
964 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
965 if (isl_tab_rollback(tabs[i], snap_i) < 0)
966 goto error;
968 if (tabs[i]->empty)
969 break;
970 if (!wraps.mat->n_row)
971 break;
973 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
974 isl_int_add_ui(bound->el[0], bound->el[0], 1);
975 if (isl_tab_add_eq(tabs[j], bound->el) < 0)
976 goto error;
977 if (isl_tab_detect_redundant(tabs[j]) < 0)
978 goto error;
980 if (!tabs[j]->empty &&
981 add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
982 goto error;
984 if (isl_tab_rollback(tabs[j], snap_j) < 0)
985 goto error;
987 if (!wraps.mat->n_row)
988 break;
991 if (k == n)
992 changed = fuse(map, i, j, tabs,
993 eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
995 isl_vec_free(bound);
996 wraps_free(&wraps);
997 isl_set_free(set);
999 return changed;
1000 error:
1001 isl_vec_free(bound);
1002 wraps_free(&wraps);
1003 isl_set_free(set);
1004 return -1;
1007 /* Given two basic sets i and j such that i has no cut equalities,
1008 * check if relaxing all the cut inequalities of i by one turns
1009 * them into valid constraint for j and check if we can wrap in
1010 * the bits that are sticking out.
1011 * If so, replace the pair by their union.
1013 * We first check if all relaxed cut inequalities of i are valid for j
1014 * and then try to wrap in the intersections of the relaxed cut inequalities
1015 * with j.
1017 * During this wrapping, we consider the points of j that lie at a distance
1018 * of exactly 1 from i. In particular, we ignore the points that lie in
1019 * between this lower-dimensional space and the basic map i.
1020 * We can therefore only apply this to integer maps.
1021 * ____ _____
1022 * / ___|_ / \
1023 * / | | / |
1024 * \ | | => \ |
1025 * \|____| \ |
1026 * \___| \____/
1028 * _____ ______
1029 * | ____|_ | \
1030 * | | | | |
1031 * | | | => | |
1032 * |_| | | |
1033 * |_____| \______|
1035 * _______
1036 * | |
1037 * | |\ |
1038 * | | \ |
1039 * | | \ |
1040 * | | \|
1041 * | | \
1042 * | |_____\
1043 * | |
1044 * |_______|
1046 * Wrapping can fail if the result of wrapping one of the facets
1047 * around its edges does not produce any new facet constraint.
1048 * In particular, this happens when we try to wrap in unbounded sets.
1050 * _______________________________________________________________________
1052 * | ___
1053 * | | |
1054 * |_| |_________________________________________________________________
1055 * |___|
1057 * The following is not an acceptable result of coalescing the above two
1058 * sets as it includes extra integer points.
1059 * _______________________________________________________________________
1061 * |
1062 * |
1064 * \______________________________________________________________________
1066 static int can_wrap_in_set(struct isl_map *map, int i, int j,
1067 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1069 int changed = 0;
1070 int k, m;
1071 int n;
1072 int *cuts = NULL;
1074 if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
1075 ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
1076 return 0;
1078 n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
1079 if (n == 0)
1080 return 0;
1082 cuts = isl_alloc_array(map->ctx, int, n);
1083 if (!cuts)
1084 return -1;
1086 for (k = 0, m = 0; m < n; ++k) {
1087 enum isl_ineq_type type;
1089 if (ineq_i[k] != STATUS_CUT)
1090 continue;
1092 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1093 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
1094 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
1095 if (type == isl_ineq_error)
1096 goto error;
1097 if (type != isl_ineq_redundant)
1098 break;
1099 cuts[m] = k;
1100 ++m;
1103 if (m == n)
1104 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
1105 eq_i, ineq_i, eq_j, ineq_j);
1107 free(cuts);
1109 return changed;
1110 error:
1111 free(cuts);
1112 return -1;
1115 /* Check if either i or j has a single cut constraint that can
1116 * be used to wrap in (a facet of) the other basic set.
1117 * if so, replace the pair by their union.
1119 static int check_wrap(struct isl_map *map, int i, int j,
1120 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1122 int changed = 0;
1124 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1125 changed = can_wrap_in_set(map, i, j, tabs,
1126 eq_i, ineq_i, eq_j, ineq_j);
1127 if (changed)
1128 return changed;
1130 if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1131 changed = can_wrap_in_set(map, j, i, tabs,
1132 eq_j, ineq_j, eq_i, ineq_i);
1133 return changed;
1136 /* At least one of the basic maps has an equality that is adjacent
1137 * to inequality. Make sure that only one of the basic maps has
1138 * such an equality and that the other basic map has exactly one
1139 * inequality adjacent to an equality.
1140 * We call the basic map that has the inequality "i" and the basic
1141 * map that has the equality "j".
1142 * If "i" has any "cut" (in)equality, then relaxing the inequality
1143 * by one would not result in a basic map that contains the other
1144 * basic map.
1146 static int check_adj_eq(struct isl_map *map, int i, int j,
1147 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1149 int changed = 0;
1150 int k;
1152 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
1153 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
1154 /* ADJ EQ TOO MANY */
1155 return 0;
1157 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
1158 return check_adj_eq(map, j, i, tabs,
1159 eq_j, ineq_j, eq_i, ineq_i);
1161 /* j has an equality adjacent to an inequality in i */
1163 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
1164 return 0;
1165 if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
1166 /* ADJ EQ CUT */
1167 return 0;
1168 if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
1169 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
1170 any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1171 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
1172 /* ADJ EQ TOO MANY */
1173 return 0;
1175 for (k = 0; k < map->p[i]->n_ineq; ++k)
1176 if (ineq_i[k] == STATUS_ADJ_EQ)
1177 break;
1179 changed = is_adj_eq_extension(map, i, j, k, tabs,
1180 eq_i, ineq_i, eq_j, ineq_j);
1181 if (changed)
1182 return changed;
1184 if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1)
1185 return 0;
1187 changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
1189 return changed;
1192 /* The two basic maps lie on adjacent hyperplanes. In particular,
1193 * basic map "i" has an equality that lies parallel to basic map "j".
1194 * Check if we can wrap the facets around the parallel hyperplanes
1195 * to include the other set.
1197 * We perform basically the same operations as can_wrap_in_facet,
1198 * except that we don't need to select a facet of one of the sets.
1200 * \\ \\
1201 * \\ => \\
1202 * \ \|
1204 * We only allow one equality of "i" to be adjacent to an equality of "j"
1205 * to avoid coalescing
1207 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1208 * x <= 10 and y <= 10;
1209 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1210 * y >= 5 and y <= 15 }
1212 * to
1214 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1215 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1216 * y2 <= 1 + x + y - x2 and y2 >= y and
1217 * y2 >= 1 + x + y - x2 }
1219 static int check_eq_adj_eq(struct isl_map *map, int i, int j,
1220 struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
1222 int k;
1223 int changed = 0;
1224 struct isl_wraps wraps;
1225 isl_mat *mat;
1226 struct isl_set *set_i = NULL;
1227 struct isl_set *set_j = NULL;
1228 struct isl_vec *bound = NULL;
1229 unsigned total = isl_basic_map_total_dim(map->p[i]);
1231 if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
1232 return 0;
1234 for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
1235 if (eq_i[k] == STATUS_ADJ_EQ)
1236 break;
1238 set_i = set_from_updated_bmap(map->p[i], tabs[i]);
1239 set_j = set_from_updated_bmap(map->p[j], tabs[j]);
1240 mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
1241 map->p[i]->n_ineq + map->p[j]->n_ineq,
1242 1 + total);
1243 wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
1244 bound = isl_vec_alloc(map->ctx, 1 + total);
1245 if (!set_i || !set_j || !wraps.mat || !bound)
1246 goto error;
1248 if (k % 2 == 0)
1249 isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
1250 else
1251 isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
1252 isl_int_add_ui(bound->el[0], bound->el[0], 1);
1254 isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
1255 wraps.mat->n_row = 1;
1257 if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
1258 goto error;
1259 if (!wraps.mat->n_row)
1260 goto unbounded;
1262 isl_int_sub_ui(bound->el[0], bound->el[0], 1);
1263 isl_seq_neg(bound->el, bound->el, 1 + total);
1265 isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
1266 wraps.mat->n_row++;
1268 if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
1269 goto error;
1270 if (!wraps.mat->n_row)
1271 goto unbounded;
1273 changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
1275 if (0) {
1276 error: changed = -1;
1278 unbounded:
1280 wraps_free(&wraps);
1281 isl_set_free(set_i);
1282 isl_set_free(set_j);
1283 isl_vec_free(bound);
1285 return changed;
1288 /* Check if the union of the given pair of basic maps
1289 * can be represented by a single basic map.
1290 * If so, replace the pair by the single basic map and return 1.
1291 * Otherwise, return 0;
1292 * The two basic maps are assumed to live in the same local space.
1294 * We first check the effect of each constraint of one basic map
1295 * on the other basic map.
1296 * The constraint may be
1297 * redundant the constraint is redundant in its own
1298 * basic map and should be ignore and removed
1299 * in the end
1300 * valid all (integer) points of the other basic map
1301 * satisfy the constraint
1302 * separate no (integer) point of the other basic map
1303 * satisfies the constraint
1304 * cut some but not all points of the other basic map
1305 * satisfy the constraint
1306 * adj_eq the given constraint is adjacent (on the outside)
1307 * to an equality of the other basic map
1308 * adj_ineq the given constraint is adjacent (on the outside)
1309 * to an inequality of the other basic map
1311 * We consider seven cases in which we can replace the pair by a single
1312 * basic map. We ignore all "redundant" constraints.
1314 * 1. all constraints of one basic map are valid
1315 * => the other basic map is a subset and can be removed
1317 * 2. all constraints of both basic maps are either "valid" or "cut"
1318 * and the facets corresponding to the "cut" constraints
1319 * of one of the basic maps lies entirely inside the other basic map
1320 * => the pair can be replaced by a basic map consisting
1321 * of the valid constraints in both basic maps
1323 * 3. there is a single pair of adjacent inequalities
1324 * (all other constraints are "valid")
1325 * => the pair can be replaced by a basic map consisting
1326 * of the valid constraints in both basic maps
1328 * 4. one basic map has a single adjacent inequality, while the other
1329 * constraints are "valid". The other basic map has some
1330 * "cut" constraints, but replacing the adjacent inequality by
1331 * its opposite and adding the valid constraints of the other
1332 * basic map results in a subset of the other basic map
1333 * => the pair can be replaced by a basic map consisting
1334 * of the valid constraints in both basic maps
1336 * 5. there is a single adjacent pair of an inequality and an equality,
1337 * the other constraints of the basic map containing the inequality are
1338 * "valid". Moreover, if the inequality the basic map is relaxed
1339 * and then turned into an equality, then resulting facet lies
1340 * entirely inside the other basic map
1341 * => the pair can be replaced by the basic map containing
1342 * the inequality, with the inequality relaxed.
1344 * 6. there is a single adjacent pair of an inequality and an equality,
1345 * the other constraints of the basic map containing the inequality are
1346 * "valid". Moreover, the facets corresponding to both
1347 * the inequality and the equality can be wrapped around their
1348 * ridges to include the other basic map
1349 * => the pair can be replaced by a basic map consisting
1350 * of the valid constraints in both basic maps together
1351 * with all wrapping constraints
1353 * 7. one of the basic maps extends beyond the other by at most one.
1354 * Moreover, the facets corresponding to the cut constraints and
1355 * the pieces of the other basic map at offset one from these cut
1356 * constraints can be wrapped around their ridges to include
1357 * the union of the two basic maps
1358 * => the pair can be replaced by a basic map consisting
1359 * of the valid constraints in both basic maps together
1360 * with all wrapping constraints
1362 * 8. the two basic maps live in adjacent hyperplanes. In principle
1363 * such sets can always be combined through wrapping, but we impose
1364 * that there is only one such pair, to avoid overeager coalescing.
1366 * Throughout the computation, we maintain a collection of tableaus
1367 * corresponding to the basic maps. When the basic maps are dropped
1368 * or combined, the tableaus are modified accordingly.
1370 static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
1371 struct isl_tab **tabs)
1373 int changed = 0;
1374 int *eq_i = NULL;
1375 int *eq_j = NULL;
1376 int *ineq_i = NULL;
1377 int *ineq_j = NULL;
1379 eq_i = eq_status_in(map->p[i], tabs[j]);
1380 if (map->p[i]->n_eq && !eq_i)
1381 goto error;
1382 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1383 goto error;
1384 if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1385 goto done;
1387 eq_j = eq_status_in(map->p[j], tabs[i]);
1388 if (map->p[j]->n_eq && !eq_j)
1389 goto error;
1390 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1391 goto error;
1392 if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1393 goto done;
1395 ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
1396 if (map->p[i]->n_ineq && !ineq_i)
1397 goto error;
1398 if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1399 goto error;
1400 if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1401 goto done;
1403 ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
1404 if (map->p[j]->n_ineq && !ineq_j)
1405 goto error;
1406 if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1407 goto error;
1408 if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1409 goto done;
1411 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1412 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1413 drop(map, j, tabs);
1414 changed = 1;
1415 } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1416 all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1417 drop(map, i, tabs);
1418 changed = 1;
1419 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
1420 changed = check_eq_adj_eq(map, i, j, tabs,
1421 eq_i, ineq_i, eq_j, ineq_j);
1422 } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1423 changed = check_eq_adj_eq(map, j, i, tabs,
1424 eq_j, ineq_j, eq_i, ineq_i);
1425 } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1426 any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1427 changed = check_adj_eq(map, i, j, tabs,
1428 eq_i, ineq_i, eq_j, ineq_j);
1429 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1430 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1431 /* Can't happen */
1432 /* BAD ADJ INEQ */
1433 } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1434 any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1435 changed = check_adj_ineq(map, i, j, tabs,
1436 eq_i, ineq_i, eq_j, ineq_j);
1437 } else {
1438 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1439 !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1440 changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1441 if (!changed)
1442 changed = check_wrap(map, i, j, tabs,
1443 eq_i, ineq_i, eq_j, ineq_j);
1446 done:
1447 free(eq_i);
1448 free(eq_j);
1449 free(ineq_i);
1450 free(ineq_j);
1451 return changed;
1452 error:
1453 free(eq_i);
1454 free(eq_j);
1455 free(ineq_i);
1456 free(ineq_j);
1457 return -1;
1460 /* Do the two basic maps live in the same local space, i.e.,
1461 * do they have the same (known) divs?
1462 * If either basic map has any unknown divs, then we can only assume
1463 * that they do not live in the same local space.
1465 static int same_divs(__isl_keep isl_basic_map *bmap1,
1466 __isl_keep isl_basic_map *bmap2)
1468 int i;
1469 int known;
1470 int total;
1472 if (!bmap1 || !bmap2)
1473 return -1;
1474 if (bmap1->n_div != bmap2->n_div)
1475 return 0;
1477 if (bmap1->n_div == 0)
1478 return 1;
1480 known = isl_basic_map_divs_known(bmap1);
1481 if (known < 0 || !known)
1482 return known;
1483 known = isl_basic_map_divs_known(bmap2);
1484 if (known < 0 || !known)
1485 return known;
1487 total = isl_basic_map_total_dim(bmap1);
1488 for (i = 0; i < bmap1->n_div; ++i)
1489 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
1490 return 0;
1492 return 1;
1495 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1496 * of those of "j", check if basic map "j" is a subset of basic map "i"
1497 * and, if so, drop basic map "j".
1499 * We first expand the divs of basic map "i" to match those of basic map "j",
1500 * using the divs and expansion computed by the caller.
1501 * Then we check if all constraints of the expanded "i" are valid for "j".
1503 static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
1504 struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
1506 isl_basic_map *bmap;
1507 int changed = 0;
1508 int *eq_i = NULL;
1509 int *ineq_i = NULL;
1511 bmap = isl_basic_map_copy(map->p[i]);
1512 bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
1514 if (!bmap)
1515 goto error;
1517 eq_i = eq_status_in(bmap, tabs[j]);
1518 if (bmap->n_eq && !eq_i)
1519 goto error;
1520 if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
1521 goto error;
1522 if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
1523 goto done;
1525 ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
1526 if (bmap->n_ineq && !ineq_i)
1527 goto error;
1528 if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
1529 goto error;
1530 if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
1531 goto done;
1533 if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1534 all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1535 drop(map, j, tabs);
1536 changed = 1;
1539 done:
1540 isl_basic_map_free(bmap);
1541 free(eq_i);
1542 free(ineq_i);
1543 return 0;
1544 error:
1545 isl_basic_map_free(bmap);
1546 free(eq_i);
1547 free(ineq_i);
1548 return -1;
1551 /* Check if the basic map "j" is a subset of basic map "i",
1552 * assuming that "i" has fewer divs that "j".
1553 * If not, then we change the order.
1555 * If the two basic maps have the same number of divs, then
1556 * they must necessarily be different. Otherwise, we would have
1557 * called coalesce_local_pair. We therefore don't try anything
1558 * in this case.
1560 * We first check if the divs of "i" are all known and form a subset
1561 * of those of "j". If so, we pass control over to coalesce_subset.
1563 static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
1564 struct isl_tab **tabs)
1566 int known;
1567 isl_mat *div_i, *div_j, *div;
1568 int *exp1 = NULL;
1569 int *exp2 = NULL;
1570 isl_ctx *ctx;
1571 int subset;
1573 if (map->p[i]->n_div == map->p[j]->n_div)
1574 return 0;
1575 if (map->p[j]->n_div < map->p[i]->n_div)
1576 return check_coalesce_subset(map, j, i, tabs);
1578 known = isl_basic_map_divs_known(map->p[i]);
1579 if (known < 0 || !known)
1580 return known;
1582 ctx = isl_map_get_ctx(map);
1584 div_i = isl_basic_map_get_divs(map->p[i]);
1585 div_j = isl_basic_map_get_divs(map->p[j]);
1587 if (!div_i || !div_j)
1588 goto error;
1590 exp1 = isl_alloc_array(ctx, int, div_i->n_row);
1591 exp2 = isl_alloc_array(ctx, int, div_j->n_row);
1592 if ((div_i->n_row && !exp1) || (div_j->n_row && !exp2))
1593 goto error;
1595 div = isl_merge_divs(div_i, div_j, exp1, exp2);
1596 if (!div)
1597 goto error;
1599 if (div->n_row == div_j->n_row)
1600 subset = coalesce_subset(map, i, j, tabs, div, exp1);
1601 else
1602 subset = 0;
1604 isl_mat_free(div);
1606 isl_mat_free(div_i);
1607 isl_mat_free(div_j);
1609 free(exp2);
1610 free(exp1);
1612 return subset;
1613 error:
1614 isl_mat_free(div_i);
1615 isl_mat_free(div_j);
1616 free(exp1);
1617 free(exp2);
1618 return -1;
1621 /* Check if the union of the given pair of basic maps
1622 * can be represented by a single basic map.
1623 * If so, replace the pair by the single basic map and return 1.
1624 * Otherwise, return 0;
1626 * We first check if the two basic maps live in the same local space.
1627 * If so, we do the complete check. Otherwise, we check if one is
1628 * an obvious subset of the other.
1630 static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
1631 struct isl_tab **tabs)
1633 int same;
1635 same = same_divs(map->p[i], map->p[j]);
1636 if (same < 0)
1637 return -1;
1638 if (same)
1639 return coalesce_local_pair(map, i, j, tabs);
1641 return check_coalesce_subset(map, i, j, tabs);
1644 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1646 int i, j;
1648 for (i = map->n - 2; i >= 0; --i)
1649 restart:
1650 for (j = i + 1; j < map->n; ++j) {
1651 int changed;
1652 changed = coalesce_pair(map, i, j, tabs);
1653 if (changed < 0)
1654 goto error;
1655 if (changed)
1656 goto restart;
1658 return map;
1659 error:
1660 isl_map_free(map);
1661 return NULL;
1664 /* For each pair of basic maps in the map, check if the union of the two
1665 * can be represented by a single basic map.
1666 * If so, replace the pair by the single basic map and start over.
1668 * Since we are constructing the tableaus of the basic maps anyway,
1669 * we exploit them to detect implicit equalities and redundant constraints.
1670 * This also helps the coalescing as it can ignore the redundant constraints.
1671 * In order to avoid confusion, we make all implicit equalities explicit
1672 * in the basic maps. We don't call isl_basic_map_gauss, though,
1673 * as that may affect the number of constraints.
1674 * This means that we have to call isl_basic_map_gauss at the end
1675 * of the computation to ensure that the basic maps are not left
1676 * in an unexpected state.
1678 struct isl_map *isl_map_coalesce(struct isl_map *map)
1680 int i;
1681 unsigned n;
1682 struct isl_tab **tabs = NULL;
1684 map = isl_map_remove_empty_parts(map);
1685 if (!map)
1686 return NULL;
1688 if (map->n <= 1)
1689 return map;
1691 map = isl_map_sort_divs(map);
1692 map = isl_map_cow(map);
1694 if (!map)
1695 return NULL;
1697 tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1698 if (!tabs)
1699 goto error;
1701 n = map->n;
1702 for (i = 0; i < map->n; ++i) {
1703 tabs[i] = isl_tab_from_basic_map(map->p[i], 0);
1704 if (!tabs[i])
1705 goto error;
1706 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1707 if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
1708 goto error;
1709 map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
1710 map->p[i]);
1711 if (!map->p[i])
1712 goto error;
1713 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1714 if (isl_tab_detect_redundant(tabs[i]) < 0)
1715 goto error;
1717 for (i = map->n - 1; i >= 0; --i)
1718 if (tabs[i]->empty)
1719 drop(map, i, tabs);
1721 map = coalesce(map, tabs);
1723 if (map)
1724 for (i = 0; i < map->n; ++i) {
1725 map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1726 tabs[i]);
1727 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1728 map->p[i] = isl_basic_map_finalize(map->p[i]);
1729 if (!map->p[i])
1730 goto error;
1731 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1732 ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1735 for (i = 0; i < n; ++i)
1736 isl_tab_free(tabs[i]);
1738 free(tabs);
1740 return map;
1741 error:
1742 if (tabs)
1743 for (i = 0; i < n; ++i)
1744 isl_tab_free(tabs[i]);
1745 free(tabs);
1746 isl_map_free(map);
1747 return NULL;
1750 /* For each pair of basic sets in the set, check if the union of the two
1751 * can be represented by a single basic set.
1752 * If so, replace the pair by the single basic set and start over.
1754 struct isl_set *isl_set_coalesce(struct isl_set *set)
1756 return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);