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"
17 #include <isl/options.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
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
);
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
)
55 int *eq
= isl_calloc_array(bmap_i
->ctx
, int, 2 * bmap_i
->n_eq
);
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
)
69 if (eq
[2 * k
] == STATUS_SEPARATE
||
70 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
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
)
88 unsigned n_eq
= bmap_i
->n_eq
;
89 int *ineq
= isl_calloc_array(bmap_i
->ctx
, int, bmap_i
->n_ineq
);
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
;
99 ineq
[k
] = status_in(bmap_i
->ineq
[k
], tab_j
);
100 if (ineq
[k
] == STATUS_ERROR
)
102 if (ineq
[k
] == STATUS_SEPARATE
)
112 static int any(int *con
, unsigned len
, int status
)
116 for (i
= 0; i
< len
; ++i
)
117 if (con
[i
] == status
)
122 static int count(int *con
, unsigned len
, int status
)
127 for (i
= 0; i
< len
; ++i
)
128 if (con
[i
] == status
)
133 static int all(int *con
, unsigned len
, int status
)
137 for (i
= 0; i
< len
; ++i
) {
138 if (con
[i
] == STATUS_REDUNDANT
)
140 if (con
[i
] != status
)
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
;
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 constraints
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
)
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
),
175 map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
,
176 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
+ extra_rows
);
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
))
184 l
= isl_basic_map_alloc_equality(fused
);
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
))
194 l
= isl_basic_map_alloc_equality(fused
);
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
)
203 l
= isl_basic_map_alloc_inequality(fused
);
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
)
212 l
= isl_basic_map_alloc_inequality(fused
);
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
);
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
);
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)
242 isl_basic_map_free(map
->p
[i
]);
244 isl_tab_free(tabs
[i
]);
250 isl_tab_free(fused_tab
);
251 isl_basic_map_free(fused
);
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
274 static int check_facets(struct isl_map
*map
, int i
, int j
,
275 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
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
)
286 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
288 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
290 if (ineq_j
[l
] != STATUS_CUT
)
292 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
293 if (stat
!= STATUS_VALID
)
296 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
298 if (l
< map
->p
[j
]->n_ineq
)
302 if (k
< map
->p
[i
]->n_ineq
)
305 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
308 /* Check if basic map "i" contains the basic map represented
309 * by the tableau "tab".
311 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
317 dim
= isl_basic_map_total_dim(map
->p
[i
]);
318 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
319 for (l
= 0; l
< 2; ++l
) {
321 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
322 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
323 if (stat
!= STATUS_VALID
)
328 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
330 if (ineq_i
[k
] == STATUS_REDUNDANT
)
332 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
333 if (stat
!= STATUS_VALID
)
339 /* Basic map "i" has an inequality (say "k") that is adjacent
340 * to some inequality of basic map "j". All the other inequalities
342 * Check if basic map "j" forms an extension of basic map "i".
344 * Note that this function is only called if some of the equalities or
345 * inequalities of basic map "j" do cut basic map "i". The function is
346 * correct even if there are no such cut constraints, but in that case
347 * the additional checks performed by this function are overkill.
349 * In particular, we replace constraint k, say f >= 0, by constraint
350 * f <= -1, add the inequalities of "j" that are valid for "i"
351 * and check if the result is a subset of basic map "j".
352 * If so, then we know that this result is exactly equal to basic map "j"
353 * since all its constraints are valid for basic map "j".
354 * By combining the valid constraints of "i" (all equalities and all
355 * inequalities except "k") and the valid constraints of "j" we therefore
356 * obtain a basic map that is equal to their union.
357 * In this case, there is no need to perform a rollback of the tableau
358 * since it is going to be destroyed in fuse().
364 * |_______| _ |_________\
376 static int is_adj_ineq_extension(__isl_keep isl_map
*map
, int i
, int j
,
377 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
380 struct isl_tab_undo
*snap
;
381 unsigned n_eq
= map
->p
[i
]->n_eq
;
382 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
385 if (isl_tab_extend_cons(tabs
[i
], 1 + map
->p
[j
]->n_ineq
) < 0)
388 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
389 if (ineq_i
[k
] == STATUS_ADJ_INEQ
)
391 if (k
>= map
->p
[i
]->n_ineq
)
392 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
393 "ineq_i should have exactly one STATUS_ADJ_INEQ",
396 snap
= isl_tab_snap(tabs
[i
]);
398 if (isl_tab_unrestrict(tabs
[i
], n_eq
+ k
) < 0)
401 isl_seq_neg(map
->p
[i
]->ineq
[k
], map
->p
[i
]->ineq
[k
], 1 + total
);
402 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
403 r
= isl_tab_add_ineq(tabs
[i
], map
->p
[i
]->ineq
[k
]);
404 isl_seq_neg(map
->p
[i
]->ineq
[k
], map
->p
[i
]->ineq
[k
], 1 + total
);
405 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
409 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
410 if (ineq_j
[k
] != STATUS_VALID
)
412 if (isl_tab_add_ineq(tabs
[i
], map
->p
[j
]->ineq
[k
]) < 0)
416 if (contains(map
, j
, ineq_j
, tabs
[i
]))
417 return fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, NULL
);
419 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
426 /* Both basic maps have at least one inequality with and adjacent
427 * (but opposite) inequality in the other basic map.
428 * Check that there are no cut constraints and that there is only
429 * a single pair of adjacent inequalities.
430 * If so, we can replace the pair by a single basic map described
431 * by all but the pair of adjacent inequalities.
432 * Any additional points introduced lie strictly between the two
433 * adjacent hyperplanes and can therefore be integral.
442 * The test for a single pair of adjancent inequalities is important
443 * for avoiding the combination of two basic maps like the following
453 * If there are some cut constraints on one side, then we may
454 * still be able to fuse the two basic maps, but we need to perform
455 * some additional checks in is_adj_ineq_extension.
457 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
458 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
460 int count_i
, count_j
;
463 count_i
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
);
464 count_j
= count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
);
466 if (count_i
!= 1 && count_j
!= 1)
469 cut_i
= any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) ||
470 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
471 cut_j
= any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
) ||
472 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
);
474 if (!cut_i
&& !cut_j
&& count_i
== 1 && count_j
== 1)
475 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
477 if (count_i
== 1 && !cut_i
)
478 return is_adj_ineq_extension(map
, i
, j
, tabs
,
479 eq_i
, ineq_i
, eq_j
, ineq_j
);
481 if (count_j
== 1 && !cut_j
)
482 return is_adj_ineq_extension(map
, j
, i
, tabs
,
483 eq_j
, ineq_j
, eq_i
, ineq_i
);
488 /* Basic map "i" has an inequality "k" that is adjacent to some equality
489 * of basic map "j". All the other inequalities are valid for "j".
490 * Check if basic map "j" forms an extension of basic map "i".
492 * In particular, we relax constraint "k", compute the corresponding
493 * facet and check whether it is included in the other basic map.
494 * If so, we know that relaxing the constraint extends the basic
495 * map with exactly the other basic map (we already know that this
496 * other basic map is included in the extension, because there
497 * were no "cut" inequalities in "i") and we can replace the
498 * two basic maps by this extension.
506 static int is_adj_eq_extension(struct isl_map
*map
, int i
, int j
, int k
,
507 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
511 struct isl_tab_undo
*snap
, *snap2
;
512 unsigned n_eq
= map
->p
[i
]->n_eq
;
514 if (isl_tab_is_equality(tabs
[i
], n_eq
+ k
))
517 snap
= isl_tab_snap(tabs
[i
]);
518 if (isl_tab_relax(tabs
[i
], n_eq
+ k
) < 0)
520 snap2
= isl_tab_snap(tabs
[i
]);
521 if (isl_tab_select_facet(tabs
[i
], n_eq
+ k
) < 0)
523 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
525 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
527 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
530 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
531 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
535 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
541 /* Data structure that keeps track of the wrapping constraints
542 * and of information to bound the coefficients of those constraints.
544 * bound is set if we want to apply a bound on the coefficients
545 * mat contains the wrapping constraints
546 * max is the bound on the coefficients (if bound is set)
554 /* Update wraps->max to be greater than or equal to the coefficients
555 * in the equalities and inequalities of bmap that can be removed if we end up
558 static void wraps_update_max(struct isl_wraps
*wraps
,
559 __isl_keep isl_basic_map
*bmap
, int *eq
, int *ineq
)
563 unsigned total
= isl_basic_map_total_dim(bmap
);
567 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
568 if (eq
[2 * k
] == STATUS_VALID
&&
569 eq
[2 * k
+ 1] == STATUS_VALID
)
571 isl_seq_abs_max(bmap
->eq
[k
] + 1, total
, &max_k
);
572 if (isl_int_abs_gt(max_k
, wraps
->max
))
573 isl_int_set(wraps
->max
, max_k
);
576 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
577 if (ineq
[k
] == STATUS_VALID
|| ineq
[k
] == STATUS_REDUNDANT
)
579 isl_seq_abs_max(bmap
->ineq
[k
] + 1, total
, &max_k
);
580 if (isl_int_abs_gt(max_k
, wraps
->max
))
581 isl_int_set(wraps
->max
, max_k
);
584 isl_int_clear(max_k
);
587 /* Initialize the isl_wraps data structure.
588 * If we want to bound the coefficients of the wrapping constraints,
589 * we set wraps->max to the largest coefficient
590 * in the equalities and inequalities that can be removed if we end up
593 static void wraps_init(struct isl_wraps
*wraps
, __isl_take isl_mat
*mat
,
594 __isl_keep isl_map
*map
, int i
, int j
,
595 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
603 ctx
= isl_mat_get_ctx(mat
);
604 wraps
->bound
= isl_options_get_coalesce_bounded_wrapping(ctx
);
607 isl_int_init(wraps
->max
);
608 isl_int_set_si(wraps
->max
, 0);
609 wraps_update_max(wraps
, map
->p
[i
], eq_i
, ineq_i
);
610 wraps_update_max(wraps
, map
->p
[j
], eq_j
, ineq_j
);
613 /* Free the contents of the isl_wraps data structure.
615 static void wraps_free(struct isl_wraps
*wraps
)
617 isl_mat_free(wraps
->mat
);
619 isl_int_clear(wraps
->max
);
622 /* Is the wrapping constraint in row "row" allowed?
624 * If wraps->bound is set, we check that none of the coefficients
625 * is greater than wraps->max.
627 static int allow_wrap(struct isl_wraps
*wraps
, int row
)
634 for (i
= 1; i
< wraps
->mat
->n_col
; ++i
)
635 if (isl_int_abs_gt(wraps
->mat
->row
[row
][i
], wraps
->max
))
641 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
642 * wrap the constraint around "bound" such that it includes the whole
643 * set "set" and append the resulting constraint to "wraps".
644 * "wraps" is assumed to have been pre-allocated to the appropriate size.
645 * wraps->n_row is the number of actual wrapped constraints that have
647 * If any of the wrapping problems results in a constraint that is
648 * identical to "bound", then this means that "set" is unbounded in such
649 * way that no wrapping is possible. If this happens then wraps->n_row
651 * Similarly, if we want to bound the coefficients of the wrapping
652 * constraints and a newly added wrapping constraint does not
653 * satisfy the bound, then wraps->n_row is also reset to zero.
655 static int add_wraps(struct isl_wraps
*wraps
, __isl_keep isl_basic_map
*bmap
,
656 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
660 unsigned total
= isl_basic_map_total_dim(bmap
);
662 w
= wraps
->mat
->n_row
;
664 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
665 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
667 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
669 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
672 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
673 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->ineq
[l
]))
675 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
677 if (!allow_wrap(wraps
, w
))
681 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
682 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
684 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
687 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
688 isl_seq_neg(wraps
->mat
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
689 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
],
690 wraps
->mat
->row
[w
+ 1]))
692 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
694 if (!allow_wrap(wraps
, w
))
698 isl_seq_cpy(wraps
->mat
->row
[w
], bound
, 1 + total
);
699 if (!isl_set_wrap_facet(set
, wraps
->mat
->row
[w
], bmap
->eq
[l
]))
701 if (isl_seq_eq(wraps
->mat
->row
[w
], bound
, 1 + total
))
703 if (!allow_wrap(wraps
, w
))
708 wraps
->mat
->n_row
= w
;
711 wraps
->mat
->n_row
= 0;
715 /* Check if the constraints in "wraps" from "first" until the last
716 * are all valid for the basic set represented by "tab".
717 * If not, wraps->n_row is set to zero.
719 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
724 for (i
= first
; i
< wraps
->n_row
; ++i
) {
725 enum isl_ineq_type type
;
726 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
727 if (type
== isl_ineq_error
)
729 if (type
== isl_ineq_redundant
)
738 /* Return a set that corresponds to the non-redundant constraints
739 * (as recorded in tab) of bmap.
741 * It's important to remove the redundant constraints as some
742 * of the other constraints may have been modified after the
743 * constraints were marked redundant.
744 * In particular, a constraint may have been relaxed.
745 * Redundant constraints are ignored when a constraint is relaxed
746 * and should therefore continue to be ignored ever after.
747 * Otherwise, the relaxation might be thwarted by some of
750 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
753 bmap
= isl_basic_map_copy(bmap
);
754 bmap
= isl_basic_map_cow(bmap
);
755 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
756 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
759 /* Given a basic set i with a constraint k that is adjacent to
760 * basic set j, check if we can wrap
761 * both the facet corresponding to k and basic map j
762 * around their ridges to include the other set.
763 * If so, replace the pair of basic sets by their union.
765 * All constraints of i (except k) are assumed to be valid for j.
766 * This means that there is no real need to wrap the ridges of
767 * the faces of basic map i around basic map j but since we do,
768 * we have to check that the resulting wrapping constraints are valid for i.
777 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
778 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
781 struct isl_wraps wraps
;
783 struct isl_set
*set_i
= NULL
;
784 struct isl_set
*set_j
= NULL
;
785 struct isl_vec
*bound
= NULL
;
786 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
787 struct isl_tab_undo
*snap
;
790 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
791 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
792 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
793 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
795 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
796 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
797 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
800 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
801 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
803 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
804 wraps
.mat
->n_row
= 1;
806 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
808 if (!wraps
.mat
->n_row
)
811 snap
= isl_tab_snap(tabs
[i
]);
813 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
) < 0)
815 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
818 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
820 n
= wraps
.mat
->n_row
;
821 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
824 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
826 if (check_wraps(wraps
.mat
, n
, tabs
[i
]) < 0)
828 if (!wraps
.mat
->n_row
)
831 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
850 /* Set the is_redundant property of the "n" constraints in "cuts",
852 * This is a fairly tricky operation as it bypasses isl_tab.c.
853 * The reason we want to temporarily mark some constraints redundant
854 * is that we want to ignore them in add_wraps.
856 * Initially all cut constraints are non-redundant, but the
857 * selection of a facet right before the call to this function
858 * may have made some of them redundant.
859 * Likewise, the same constraints are marked non-redundant
860 * in the second call to this function, before they are officially
861 * made non-redundant again in the subsequent rollback.
863 static void set_is_redundant(struct isl_tab
*tab
, unsigned n_eq
,
864 int *cuts
, int n
, int k
, int v
)
868 for (l
= 0; l
< n
; ++l
) {
871 tab
->con
[n_eq
+ cuts
[l
]].is_redundant
= v
;
875 /* Given a pair of basic maps i and j such that j sticks out
876 * of i at n cut constraints, each time by at most one,
877 * try to compute wrapping constraints and replace the two
878 * basic maps by a single basic map.
879 * The other constraints of i are assumed to be valid for j.
881 * The facets of i corresponding to the cut constraints are
882 * wrapped around their ridges, except those ridges determined
883 * by any of the other cut constraints.
884 * The intersections of cut constraints need to be ignored
885 * as the result of wrapping one cut constraint around another
886 * would result in a constraint cutting the union.
887 * In each case, the facets are wrapped to include the union
888 * of the two basic maps.
890 * The pieces of j that lie at an offset of exactly one from
891 * one of the cut constraints of i are wrapped around their edges.
892 * Here, there is no need to ignore intersections because we
893 * are wrapping around the union of the two basic maps.
895 * If any wrapping fails, i.e., if we cannot wrap to touch
896 * the union, then we give up.
897 * Otherwise, the pair of basic maps is replaced by their union.
899 static int wrap_in_facets(struct isl_map
*map
, int i
, int j
,
900 int *cuts
, int n
, struct isl_tab
**tabs
,
901 int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
904 struct isl_wraps wraps
;
907 isl_vec
*bound
= NULL
;
908 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
911 struct isl_tab_undo
*snap_i
, *snap_j
;
913 if (isl_tab_extend_cons(tabs
[j
], 1) < 0)
916 max_wrap
= 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
917 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
;
920 set
= isl_set_union(set_from_updated_bmap(map
->p
[i
], tabs
[i
]),
921 set_from_updated_bmap(map
->p
[j
], tabs
[j
]));
922 mat
= isl_mat_alloc(map
->ctx
, max_wrap
, 1 + total
);
923 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
924 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
925 if (!set
|| !wraps
.mat
|| !bound
)
928 snap_i
= isl_tab_snap(tabs
[i
]);
929 snap_j
= isl_tab_snap(tabs
[j
]);
931 wraps
.mat
->n_row
= 0;
933 for (k
= 0; k
< n
; ++k
) {
934 if (isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ cuts
[k
]) < 0)
936 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
938 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 1);
940 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
941 if (!tabs
[i
]->empty
&&
942 add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set
) < 0)
945 set_is_redundant(tabs
[i
], map
->p
[i
]->n_eq
, cuts
, n
, k
, 0);
946 if (isl_tab_rollback(tabs
[i
], snap_i
) < 0)
951 if (!wraps
.mat
->n_row
)
954 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[cuts
[k
]], 1 + total
);
955 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
956 if (isl_tab_add_eq(tabs
[j
], bound
->el
) < 0)
958 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
961 if (!tabs
[j
]->empty
&&
962 add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set
) < 0)
965 if (isl_tab_rollback(tabs
[j
], snap_j
) < 0)
968 if (!wraps
.mat
->n_row
)
973 changed
= fuse(map
, i
, j
, tabs
,
974 eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
988 /* Given two basic sets i and j such that i has no cut equalities,
989 * check if relaxing all the cut inequalities of i by one turns
990 * them into valid constraint for j and check if we can wrap in
991 * the bits that are sticking out.
992 * If so, replace the pair by their union.
994 * We first check if all relaxed cut inequalities of i are valid for j
995 * and then try to wrap in the intersections of the relaxed cut inequalities
998 * During this wrapping, we consider the points of j that lie at a distance
999 * of exactly 1 from i. In particular, we ignore the points that lie in
1000 * between this lower-dimensional space and the basic map i.
1001 * We can therefore only apply this to integer maps.
1027 * Wrapping can fail if the result of wrapping one of the facets
1028 * around its edges does not produce any new facet constraint.
1029 * In particular, this happens when we try to wrap in unbounded sets.
1031 * _______________________________________________________________________
1035 * |_| |_________________________________________________________________
1038 * The following is not an acceptable result of coalescing the above two
1039 * sets as it includes extra integer points.
1040 * _______________________________________________________________________
1045 * \______________________________________________________________________
1047 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
1048 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1055 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) ||
1056 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
1059 n
= count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
);
1063 cuts
= isl_alloc_array(map
->ctx
, int, n
);
1067 for (k
= 0, m
= 0; m
< n
; ++k
) {
1068 enum isl_ineq_type type
;
1070 if (ineq_i
[k
] != STATUS_CUT
)
1073 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1074 type
= isl_tab_ineq_type(tabs
[j
], map
->p
[i
]->ineq
[k
]);
1075 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
1076 if (type
== isl_ineq_error
)
1078 if (type
!= isl_ineq_redundant
)
1085 changed
= wrap_in_facets(map
, i
, j
, cuts
, n
, tabs
,
1086 eq_i
, ineq_i
, eq_j
, ineq_j
);
1096 /* Check if either i or j has only cut inequalities that can
1097 * be used to wrap in (a facet of) the other basic set.
1098 * if so, replace the pair by their union.
1100 static int check_wrap(struct isl_map
*map
, int i
, int j
,
1101 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1105 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1106 changed
= can_wrap_in_set(map
, i
, j
, tabs
,
1107 eq_i
, ineq_i
, eq_j
, ineq_j
);
1111 if (!any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1112 changed
= can_wrap_in_set(map
, j
, i
, tabs
,
1113 eq_j
, ineq_j
, eq_i
, ineq_i
);
1117 /* At least one of the basic maps has an equality that is adjacent
1118 * to inequality. Make sure that only one of the basic maps has
1119 * such an equality and that the other basic map has exactly one
1120 * inequality adjacent to an equality.
1121 * We call the basic map that has the inequality "i" and the basic
1122 * map that has the equality "j".
1123 * If "i" has any "cut" (in)equality, then relaxing the inequality
1124 * by one would not result in a basic map that contains the other
1127 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
1128 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1133 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
1134 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
1135 /* ADJ EQ TOO MANY */
1138 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
1139 return check_adj_eq(map
, j
, i
, tabs
,
1140 eq_j
, ineq_j
, eq_i
, ineq_i
);
1142 /* j has an equality adjacent to an inequality in i */
1144 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
))
1146 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
1149 if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
1150 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
1151 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1152 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
1153 /* ADJ EQ TOO MANY */
1156 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
1157 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
1160 changed
= is_adj_eq_extension(map
, i
, j
, k
, tabs
,
1161 eq_i
, ineq_i
, eq_j
, ineq_j
);
1165 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1)
1168 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1173 /* The two basic maps lie on adjacent hyperplanes. In particular,
1174 * basic map "i" has an equality that lies parallel to basic map "j".
1175 * Check if we can wrap the facets around the parallel hyperplanes
1176 * to include the other set.
1178 * We perform basically the same operations as can_wrap_in_facet,
1179 * except that we don't need to select a facet of one of the sets.
1185 * We only allow one equality of "i" to be adjacent to an equality of "j"
1186 * to avoid coalescing
1188 * [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
1189 * x <= 10 and y <= 10;
1190 * [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
1191 * y >= 5 and y <= 15 }
1195 * [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
1196 * 4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
1197 * y2 <= 1 + x + y - x2 and y2 >= y and
1198 * y2 >= 1 + x + y - x2 }
1200 static int check_eq_adj_eq(struct isl_map
*map
, int i
, int j
,
1201 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
1205 struct isl_wraps wraps
;
1207 struct isl_set
*set_i
= NULL
;
1208 struct isl_set
*set_j
= NULL
;
1209 struct isl_vec
*bound
= NULL
;
1210 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
1212 if (count(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) != 1)
1215 for (k
= 0; k
< 2 * map
->p
[i
]->n_eq
; ++k
)
1216 if (eq_i
[k
] == STATUS_ADJ_EQ
)
1219 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
1220 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
1221 mat
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
1222 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
1224 wraps_init(&wraps
, mat
, map
, i
, j
, eq_i
, ineq_i
, eq_j
, ineq_j
);
1225 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
1226 if (!set_i
|| !set_j
|| !wraps
.mat
|| !bound
)
1230 isl_seq_neg(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1232 isl_seq_cpy(bound
->el
, map
->p
[i
]->eq
[k
/ 2], 1 + total
);
1233 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
1235 isl_seq_cpy(wraps
.mat
->row
[0], bound
->el
, 1 + total
);
1236 wraps
.mat
->n_row
= 1;
1238 if (add_wraps(&wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
1240 if (!wraps
.mat
->n_row
)
1243 isl_int_sub_ui(bound
->el
[0], bound
->el
[0], 1);
1244 isl_seq_neg(bound
->el
, bound
->el
, 1 + total
);
1246 isl_seq_cpy(wraps
.mat
->row
[wraps
.mat
->n_row
], bound
->el
, 1 + total
);
1249 if (add_wraps(&wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
1251 if (!wraps
.mat
->n_row
)
1254 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
.mat
);
1257 error
: changed
= -1;
1262 isl_set_free(set_i
);
1263 isl_set_free(set_j
);
1264 isl_vec_free(bound
);
1269 /* Check if the union of the given pair of basic maps
1270 * can be represented by a single basic map.
1271 * If so, replace the pair by the single basic map and return 1.
1272 * Otherwise, return 0;
1273 * The two basic maps are assumed to live in the same local space.
1275 * We first check the effect of each constraint of one basic map
1276 * on the other basic map.
1277 * The constraint may be
1278 * redundant the constraint is redundant in its own
1279 * basic map and should be ignore and removed
1281 * valid all (integer) points of the other basic map
1282 * satisfy the constraint
1283 * separate no (integer) point of the other basic map
1284 * satisfies the constraint
1285 * cut some but not all points of the other basic map
1286 * satisfy the constraint
1287 * adj_eq the given constraint is adjacent (on the outside)
1288 * to an equality of the other basic map
1289 * adj_ineq the given constraint is adjacent (on the outside)
1290 * to an inequality of the other basic map
1292 * We consider seven cases in which we can replace the pair by a single
1293 * basic map. We ignore all "redundant" constraints.
1295 * 1. all constraints of one basic map are valid
1296 * => the other basic map is a subset and can be removed
1298 * 2. all constraints of both basic maps are either "valid" or "cut"
1299 * and the facets corresponding to the "cut" constraints
1300 * of one of the basic maps lies entirely inside the other basic map
1301 * => the pair can be replaced by a basic map consisting
1302 * of the valid constraints in both basic maps
1304 * 3. there is a single pair of adjacent inequalities
1305 * (all other constraints are "valid")
1306 * => the pair can be replaced by a basic map consisting
1307 * of the valid constraints in both basic maps
1309 * 4. one basic map has a single adjacent inequality, while the other
1310 * constraints are "valid". The other basic map has some
1311 * "cut" constraints, but replacing the adjacent inequality by
1312 * its opposite and adding the valid constraints of the other
1313 * basic map results in a subset of the other basic map
1314 * => the pair can be replaced by a basic map consisting
1315 * of the valid constraints in both basic maps
1317 * 5. there is a single adjacent pair of an inequality and an equality,
1318 * the other constraints of the basic map containing the inequality are
1319 * "valid". Moreover, if the inequality the basic map is relaxed
1320 * and then turned into an equality, then resulting facet lies
1321 * entirely inside the other basic map
1322 * => the pair can be replaced by the basic map containing
1323 * the inequality, with the inequality relaxed.
1325 * 6. there is a single adjacent pair of an inequality and an equality,
1326 * the other constraints of the basic map containing the inequality are
1327 * "valid". Moreover, the facets corresponding to both
1328 * the inequality and the equality can be wrapped around their
1329 * ridges to include the other basic map
1330 * => the pair can be replaced by a basic map consisting
1331 * of the valid constraints in both basic maps together
1332 * with all wrapping constraints
1334 * 7. one of the basic maps extends beyond the other by at most one.
1335 * Moreover, the facets corresponding to the cut constraints and
1336 * the pieces of the other basic map at offset one from these cut
1337 * constraints can be wrapped around their ridges to include
1338 * the union of the two basic maps
1339 * => the pair can be replaced by a basic map consisting
1340 * of the valid constraints in both basic maps together
1341 * with all wrapping constraints
1343 * 8. the two basic maps live in adjacent hyperplanes. In principle
1344 * such sets can always be combined through wrapping, but we impose
1345 * that there is only one such pair, to avoid overeager coalescing.
1347 * Throughout the computation, we maintain a collection of tableaus
1348 * corresponding to the basic maps. When the basic maps are dropped
1349 * or combined, the tableaus are modified accordingly.
1351 static int coalesce_local_pair(__isl_keep isl_map
*map
, int i
, int j
,
1352 struct isl_tab
**tabs
)
1360 eq_i
= eq_status_in(map
->p
[i
], tabs
[j
]);
1361 if (map
->p
[i
]->n_eq
&& !eq_i
)
1363 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
1365 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
1368 eq_j
= eq_status_in(map
->p
[j
], tabs
[i
]);
1369 if (map
->p
[j
]->n_eq
&& !eq_j
)
1371 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
1373 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
1376 ineq_i
= ineq_status_in(map
->p
[i
], tabs
[i
], tabs
[j
]);
1377 if (map
->p
[i
]->n_ineq
&& !ineq_i
)
1379 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
1381 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
1384 ineq_j
= ineq_status_in(map
->p
[j
], tabs
[j
], tabs
[i
]);
1385 if (map
->p
[j
]->n_ineq
&& !ineq_j
)
1387 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
1389 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
1392 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1393 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1396 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
1397 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
1400 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
)) {
1401 changed
= check_eq_adj_eq(map
, i
, j
, tabs
,
1402 eq_i
, ineq_i
, eq_j
, ineq_j
);
1403 } else if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
1404 changed
= check_eq_adj_eq(map
, j
, i
, tabs
,
1405 eq_j
, ineq_j
, eq_i
, ineq_i
);
1406 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
1407 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
1408 changed
= check_adj_eq(map
, i
, j
, tabs
,
1409 eq_i
, ineq_i
, eq_j
, ineq_j
);
1410 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
1411 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
1414 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
1415 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
1416 changed
= check_adj_ineq(map
, i
, j
, tabs
,
1417 eq_i
, ineq_i
, eq_j
, ineq_j
);
1419 if (!any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) &&
1420 !any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
))
1421 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
1423 changed
= check_wrap(map
, i
, j
, tabs
,
1424 eq_i
, ineq_i
, eq_j
, ineq_j
);
1441 /* Do the two basic maps live in the same local space, i.e.,
1442 * do they have the same (known) divs?
1443 * If either basic map has any unknown divs, then we can only assume
1444 * that they do not live in the same local space.
1446 static int same_divs(__isl_keep isl_basic_map
*bmap1
,
1447 __isl_keep isl_basic_map
*bmap2
)
1453 if (!bmap1
|| !bmap2
)
1455 if (bmap1
->n_div
!= bmap2
->n_div
)
1458 if (bmap1
->n_div
== 0)
1461 known
= isl_basic_map_divs_known(bmap1
);
1462 if (known
< 0 || !known
)
1464 known
= isl_basic_map_divs_known(bmap2
);
1465 if (known
< 0 || !known
)
1468 total
= isl_basic_map_total_dim(bmap1
);
1469 for (i
= 0; i
< bmap1
->n_div
; ++i
)
1470 if (!isl_seq_eq(bmap1
->div
[i
], bmap2
->div
[i
], 2 + total
))
1476 /* Given two basic maps "i" and "j", where the divs of "i" form a subset
1477 * of those of "j", check if basic map "j" is a subset of basic map "i"
1478 * and, if so, drop basic map "j".
1480 * We first expand the divs of basic map "i" to match those of basic map "j",
1481 * using the divs and expansion computed by the caller.
1482 * Then we check if all constraints of the expanded "i" are valid for "j".
1484 static int coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1485 struct isl_tab
**tabs
, __isl_keep isl_mat
*div
, int *exp
)
1487 isl_basic_map
*bmap
;
1492 bmap
= isl_basic_map_copy(map
->p
[i
]);
1493 bmap
= isl_basic_set_expand_divs(bmap
, isl_mat_copy(div
), exp
);
1498 eq_i
= eq_status_in(bmap
, tabs
[j
]);
1499 if (bmap
->n_eq
&& !eq_i
)
1501 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_ERROR
))
1503 if (any(eq_i
, 2 * bmap
->n_eq
, STATUS_SEPARATE
))
1506 ineq_i
= ineq_status_in(bmap
, NULL
, tabs
[j
]);
1507 if (bmap
->n_ineq
&& !ineq_i
)
1509 if (any(ineq_i
, bmap
->n_ineq
, STATUS_ERROR
))
1511 if (any(ineq_i
, bmap
->n_ineq
, STATUS_SEPARATE
))
1514 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
1515 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
1521 isl_basic_map_free(bmap
);
1526 isl_basic_map_free(bmap
);
1532 /* Check if the basic map "j" is a subset of basic map "i",
1533 * assuming that "i" has fewer divs that "j".
1534 * If not, then we change the order.
1536 * If the two basic maps have the same number of divs, then
1537 * they must necessarily be different. Otherwise, we would have
1538 * called coalesce_local_pair. We therefore don't try anything
1541 * We first check if the divs of "i" are all known and form a subset
1542 * of those of "j". If so, we pass control over to coalesce_subset.
1544 static int check_coalesce_subset(__isl_keep isl_map
*map
, int i
, int j
,
1545 struct isl_tab
**tabs
)
1548 isl_mat
*div_i
, *div_j
, *div
;
1554 if (map
->p
[i
]->n_div
== map
->p
[j
]->n_div
)
1556 if (map
->p
[j
]->n_div
< map
->p
[i
]->n_div
)
1557 return check_coalesce_subset(map
, j
, i
, tabs
);
1559 known
= isl_basic_map_divs_known(map
->p
[i
]);
1560 if (known
< 0 || !known
)
1563 ctx
= isl_map_get_ctx(map
);
1565 div_i
= isl_basic_map_get_divs(map
->p
[i
]);
1566 div_j
= isl_basic_map_get_divs(map
->p
[j
]);
1568 if (!div_i
|| !div_j
)
1571 exp1
= isl_alloc_array(ctx
, int, div_i
->n_row
);
1572 exp2
= isl_alloc_array(ctx
, int, div_j
->n_row
);
1573 if ((div_i
->n_row
&& !exp1
) || (div_j
->n_row
&& !exp2
))
1576 div
= isl_merge_divs(div_i
, div_j
, exp1
, exp2
);
1580 if (div
->n_row
== div_j
->n_row
)
1581 subset
= coalesce_subset(map
, i
, j
, tabs
, div
, exp1
);
1587 isl_mat_free(div_i
);
1588 isl_mat_free(div_j
);
1595 isl_mat_free(div_i
);
1596 isl_mat_free(div_j
);
1602 /* Check if the union of the given pair of basic maps
1603 * can be represented by a single basic map.
1604 * If so, replace the pair by the single basic map and return 1.
1605 * Otherwise, return 0;
1607 * We first check if the two basic maps live in the same local space.
1608 * If so, we do the complete check. Otherwise, we check if one is
1609 * an obvious subset of the other.
1611 static int coalesce_pair(__isl_keep isl_map
*map
, int i
, int j
,
1612 struct isl_tab
**tabs
)
1616 same
= same_divs(map
->p
[i
], map
->p
[j
]);
1620 return coalesce_local_pair(map
, i
, j
, tabs
);
1622 return check_coalesce_subset(map
, i
, j
, tabs
);
1625 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
1629 for (i
= map
->n
- 2; i
>= 0; --i
)
1631 for (j
= i
+ 1; j
< map
->n
; ++j
) {
1633 changed
= coalesce_pair(map
, i
, j
, tabs
);
1645 /* For each pair of basic maps in the map, check if the union of the two
1646 * can be represented by a single basic map.
1647 * If so, replace the pair by the single basic map and start over.
1649 * Since we are constructing the tableaus of the basic maps anyway,
1650 * we exploit them to detect implicit equalities and redundant constraints.
1651 * This also helps the coalescing as it can ignore the redundant constraints.
1652 * In order to avoid confusion, we make all implicit equalities explicit
1653 * in the basic maps. We don't call isl_basic_map_gauss, though,
1654 * as that may affect the number of constraints.
1655 * This means that we have to call isl_basic_map_gauss at the end
1656 * of the computation to ensure that the basic maps are not left
1657 * in an unexpected state.
1659 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
1663 struct isl_tab
**tabs
= NULL
;
1665 map
= isl_map_remove_empty_parts(map
);
1672 map
= isl_map_sort_divs(map
);
1673 map
= isl_map_cow(map
);
1678 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
1683 for (i
= 0; i
< map
->n
; ++i
) {
1684 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
], 0);
1687 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
1688 if (isl_tab_detect_implicit_equalities(tabs
[i
]) < 0)
1690 map
->p
[i
] = isl_tab_make_equalities_explicit(tabs
[i
],
1694 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
1695 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
1698 for (i
= map
->n
- 1; i
>= 0; --i
)
1702 map
= coalesce(map
, tabs
);
1705 for (i
= 0; i
< map
->n
; ++i
) {
1706 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
1708 map
->p
[i
] = isl_basic_map_gauss(map
->p
[i
], NULL
);
1709 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1712 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
1713 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
1716 for (i
= 0; i
< n
; ++i
)
1717 isl_tab_free(tabs
[i
]);
1724 for (i
= 0; i
< n
; ++i
)
1725 isl_tab_free(tabs
[i
]);
1731 /* For each pair of basic sets in the set, check if the union of the two
1732 * can be represented by a single basic set.
1733 * If so, replace the pair by the single basic set and start over.
1735 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
1737 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);