2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include "isl_map_private.h"
17 #define STATUS_ERROR -1
18 #define STATUS_REDUNDANT 1
19 #define STATUS_VALID 2
20 #define STATUS_SEPARATE 3
22 #define STATUS_ADJ_EQ 5
23 #define STATUS_ADJ_INEQ 6
25 static int status_in(isl_int
*ineq
, struct isl_tab
*tab
)
27 enum isl_ineq_type type
= isl_tab_ineq_type(tab
, ineq
);
29 case isl_ineq_error
: return STATUS_ERROR
;
30 case isl_ineq_redundant
: return STATUS_VALID
;
31 case isl_ineq_separate
: return STATUS_SEPARATE
;
32 case isl_ineq_cut
: return STATUS_CUT
;
33 case isl_ineq_adj_eq
: return STATUS_ADJ_EQ
;
34 case isl_ineq_adj_ineq
: return STATUS_ADJ_INEQ
;
38 /* Compute the position of the equalities of basic map "i"
39 * with respect to basic map "j".
40 * The resulting array has twice as many entries as the number
41 * of equalities corresponding to the two inequalties to which
42 * each equality corresponds.
44 static int *eq_status_in(struct isl_map
*map
, int i
, int j
,
45 struct isl_tab
**tabs
)
48 int *eq
= isl_calloc_array(map
->ctx
, int, 2 * map
->p
[i
]->n_eq
);
51 dim
= isl_basic_map_total_dim(map
->p
[i
]);
52 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
53 for (l
= 0; l
< 2; ++l
) {
54 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
55 eq
[2 * k
+ l
] = status_in(map
->p
[i
]->eq
[k
], tabs
[j
]);
56 if (eq
[2 * k
+ l
] == STATUS_ERROR
)
59 if (eq
[2 * k
] == STATUS_SEPARATE
||
60 eq
[2 * k
+ 1] == STATUS_SEPARATE
)
70 /* Compute the position of the inequalities of basic map "i"
71 * with respect to basic map "j".
73 static int *ineq_status_in(struct isl_map
*map
, int i
, int j
,
74 struct isl_tab
**tabs
)
77 unsigned n_eq
= map
->p
[i
]->n_eq
;
78 int *ineq
= isl_calloc_array(map
->ctx
, int, map
->p
[i
]->n_ineq
);
80 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
81 if (isl_tab_is_redundant(tabs
[i
], n_eq
+ k
)) {
82 ineq
[k
] = STATUS_REDUNDANT
;
85 ineq
[k
] = status_in(map
->p
[i
]->ineq
[k
], tabs
[j
]);
86 if (ineq
[k
] == STATUS_ERROR
)
88 if (ineq
[k
] == STATUS_SEPARATE
)
98 static int any(int *con
, unsigned len
, int status
)
102 for (i
= 0; i
< len
; ++i
)
103 if (con
[i
] == status
)
108 static int count(int *con
, unsigned len
, int status
)
113 for (i
= 0; i
< len
; ++i
)
114 if (con
[i
] == status
)
119 static int all(int *con
, unsigned len
, int status
)
123 for (i
= 0; i
< len
; ++i
) {
124 if (con
[i
] == STATUS_REDUNDANT
)
126 if (con
[i
] != status
)
132 static void drop(struct isl_map
*map
, int i
, struct isl_tab
**tabs
)
134 isl_basic_map_free(map
->p
[i
]);
135 isl_tab_free(tabs
[i
]);
137 if (i
!= map
->n
- 1) {
138 map
->p
[i
] = map
->p
[map
->n
- 1];
139 tabs
[i
] = tabs
[map
->n
- 1];
141 tabs
[map
->n
- 1] = NULL
;
145 /* Replace the pair of basic maps i and j by the basic map bounded
146 * by the valid constraints in both basic maps and the constraint
147 * in extra (if not NULL).
149 static int fuse(struct isl_map
*map
, int i
, int j
,
150 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
,
151 __isl_keep isl_mat
*extra
)
154 struct isl_basic_map
*fused
= NULL
;
155 struct isl_tab
*fused_tab
= NULL
;
156 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
157 unsigned extra_rows
= extra
? extra
->n_row
: 0;
159 fused
= isl_basic_map_alloc_dim(isl_dim_copy(map
->p
[i
]->dim
),
161 map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
,
162 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
+ extra_rows
);
166 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
167 if (eq_i
&& (eq_i
[2 * k
] != STATUS_VALID
||
168 eq_i
[2 * k
+ 1] != STATUS_VALID
))
170 l
= isl_basic_map_alloc_equality(fused
);
173 isl_seq_cpy(fused
->eq
[l
], map
->p
[i
]->eq
[k
], 1 + total
);
176 for (k
= 0; k
< map
->p
[j
]->n_eq
; ++k
) {
177 if (eq_j
&& (eq_j
[2 * k
] != STATUS_VALID
||
178 eq_j
[2 * k
+ 1] != STATUS_VALID
))
180 l
= isl_basic_map_alloc_equality(fused
);
183 isl_seq_cpy(fused
->eq
[l
], map
->p
[j
]->eq
[k
], 1 + total
);
186 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
187 if (ineq_i
[k
] != STATUS_VALID
)
189 l
= isl_basic_map_alloc_inequality(fused
);
192 isl_seq_cpy(fused
->ineq
[l
], map
->p
[i
]->ineq
[k
], 1 + total
);
195 for (k
= 0; k
< map
->p
[j
]->n_ineq
; ++k
) {
196 if (ineq_j
[k
] != STATUS_VALID
)
198 l
= isl_basic_map_alloc_inequality(fused
);
201 isl_seq_cpy(fused
->ineq
[l
], map
->p
[j
]->ineq
[k
], 1 + total
);
204 for (k
= 0; k
< map
->p
[i
]->n_div
; ++k
) {
205 int l
= isl_basic_map_alloc_div(fused
);
208 isl_seq_cpy(fused
->div
[l
], map
->p
[i
]->div
[k
], 1 + 1 + total
);
211 for (k
= 0; k
< extra_rows
; ++k
) {
212 l
= isl_basic_map_alloc_inequality(fused
);
215 isl_seq_cpy(fused
->ineq
[l
], extra
->row
[k
], 1 + total
);
218 fused
= isl_basic_map_gauss(fused
, NULL
);
219 ISL_F_SET(fused
, ISL_BASIC_MAP_FINAL
);
220 if (ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_RATIONAL
) &&
221 ISL_F_ISSET(map
->p
[j
], ISL_BASIC_MAP_RATIONAL
))
222 ISL_F_SET(fused
, ISL_BASIC_MAP_RATIONAL
);
224 fused_tab
= isl_tab_from_basic_map(fused
);
225 if (isl_tab_detect_redundant(fused_tab
) < 0)
228 isl_basic_map_free(map
->p
[i
]);
230 isl_tab_free(tabs
[i
]);
236 isl_tab_free(fused_tab
);
237 isl_basic_map_free(fused
);
241 /* Given a pair of basic maps i and j such that all constraints are either
242 * "valid" or "cut", check if the facets corresponding to the "cut"
243 * constraints of i lie entirely within basic map j.
244 * If so, replace the pair by the basic map consisting of the valid
245 * constraints in both basic maps.
247 * To see that we are not introducing any extra points, call the
248 * two basic maps A and B and the resulting map U and let x
249 * be an element of U \setminus ( A \cup B ).
250 * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
251 * violates them. Let X be the intersection of U with the opposites
252 * of these constraints. Then x \in X.
253 * The facet corresponding to c_1 contains the corresponding facet of A.
254 * This facet is entirely contained in B, so c_2 is valid on the facet.
255 * However, since it is also (part of) a facet of X, -c_2 is also valid
256 * on the facet. This means c_2 is saturated on the facet, so c_1 and
257 * c_2 must be opposites of each other, but then x could not violate
260 static int check_facets(struct isl_map
*map
, int i
, int j
,
261 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
264 struct isl_tab_undo
*snap
;
265 unsigned n_eq
= map
->p
[i
]->n_eq
;
267 snap
= isl_tab_snap(tabs
[i
]);
269 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
270 if (ineq_i
[k
] != STATUS_CUT
)
272 tabs
[i
] = isl_tab_select_facet(tabs
[i
], n_eq
+ k
);
273 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
275 if (ineq_j
[l
] != STATUS_CUT
)
277 stat
= status_in(map
->p
[j
]->ineq
[l
], tabs
[i
]);
278 if (stat
!= STATUS_VALID
)
281 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
283 if (l
< map
->p
[j
]->n_ineq
)
287 if (k
< map
->p
[i
]->n_ineq
)
290 return fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
293 /* Both basic maps have at least one inequality with and adjacent
294 * (but opposite) inequality in the other basic map.
295 * Check that there are no cut constraints and that there is only
296 * a single pair of adjacent inequalities.
297 * If so, we can replace the pair by a single basic map described
298 * by all but the pair of adjacent inequalities.
299 * Any additional points introduced lie strictly between the two
300 * adjacent hyperplanes and can therefore be integral.
309 * The test for a single pair of adjancent inequalities is important
310 * for avoiding the combination of two basic maps like the following
320 static int check_adj_ineq(struct isl_map
*map
, int i
, int j
,
321 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
325 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
) ||
326 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
))
329 else if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) == 1 &&
330 count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
) == 1)
331 changed
= fuse(map
, i
, j
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
, NULL
);
332 /* else ADJ INEQ TOO MANY */
337 /* Check if basic map "i" contains the basic map represented
338 * by the tableau "tab".
340 static int contains(struct isl_map
*map
, int i
, int *ineq_i
,
346 dim
= isl_basic_map_total_dim(map
->p
[i
]);
347 for (k
= 0; k
< map
->p
[i
]->n_eq
; ++k
) {
348 for (l
= 0; l
< 2; ++l
) {
350 isl_seq_neg(map
->p
[i
]->eq
[k
], map
->p
[i
]->eq
[k
], 1+dim
);
351 stat
= status_in(map
->p
[i
]->eq
[k
], tab
);
352 if (stat
!= STATUS_VALID
)
357 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
) {
359 if (ineq_i
[k
] == STATUS_REDUNDANT
)
361 stat
= status_in(map
->p
[i
]->ineq
[k
], tab
);
362 if (stat
!= STATUS_VALID
)
368 /* Basic map "i" has an inequality "k" that is adjacent to some equality
369 * of basic map "j". All the other inequalities are valid for "j".
370 * Check if basic map "j" forms an extension of basic map "i".
372 * In particular, we relax constraint "k", compute the corresponding
373 * facet and check whether it is included in the other basic map.
374 * If so, we know that relaxing the constraint extends the basic
375 * map with exactly the other basic map (we already know that this
376 * other basic map is included in the extension, because there
377 * were no "cut" inequalities in "i") and we can replace the
378 * two basic maps by thie extension.
386 static int is_extension(struct isl_map
*map
, int i
, int j
, int k
,
387 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
391 struct isl_tab_undo
*snap
, *snap2
;
392 unsigned n_eq
= map
->p
[i
]->n_eq
;
394 snap
= isl_tab_snap(tabs
[i
]);
395 tabs
[i
] = isl_tab_relax(tabs
[i
], n_eq
+ k
);
396 snap2
= isl_tab_snap(tabs
[i
]);
397 tabs
[i
] = isl_tab_select_facet(tabs
[i
], n_eq
+ k
);
398 super
= contains(map
, j
, ineq_j
, tabs
[i
]);
400 if (isl_tab_rollback(tabs
[i
], snap2
) < 0)
402 map
->p
[i
] = isl_basic_map_cow(map
->p
[i
]);
405 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
406 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_FINAL
);
410 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
416 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
417 * wrap the constraint around "bound" such that it includes the whole
418 * set "set" and append the resulting constraint to "wraps".
419 * "wraps" is assumed to have been pre-allocated to the appropriate size.
420 * wraps->n_row is the number of actual wrapped constraints that have
422 * If any of the wrapping problems results in a constraint that is
423 * identical to "bound", then this means that "set" is unbounded in such
424 * way that no wrapping is possible. If this happens then wraps->n_row
427 static int add_wraps(__isl_keep isl_mat
*wraps
, __isl_keep isl_basic_map
*bmap
,
428 struct isl_tab
*tab
, isl_int
*bound
, __isl_keep isl_set
*set
)
432 unsigned total
= isl_basic_map_total_dim(bmap
);
436 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
437 if (isl_seq_is_neg(bound
, bmap
->ineq
[l
], 1 + total
))
439 if (isl_seq_eq(bound
, bmap
->ineq
[l
], 1 + total
))
441 if (isl_tab_is_redundant(tab
, bmap
->n_eq
+ l
))
444 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
445 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->ineq
[l
]))
447 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
451 for (l
= 0; l
< bmap
->n_eq
; ++l
) {
452 if (isl_seq_is_neg(bound
, bmap
->eq
[l
], 1 + total
))
454 if (isl_seq_eq(bound
, bmap
->eq
[l
], 1 + total
))
457 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
458 isl_seq_neg(wraps
->row
[w
+ 1], bmap
->eq
[l
], 1 + total
);
459 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], wraps
->row
[w
+ 1]))
461 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
465 isl_seq_cpy(wraps
->row
[w
], bound
, 1 + total
);
466 if (!isl_set_wrap_facet(set
, wraps
->row
[w
], bmap
->eq
[l
]))
468 if (isl_seq_eq(wraps
->row
[w
], bound
, 1 + total
))
480 /* Check if the constraints in "wraps" from "first" until the last
481 * are all valid for the basic set represented by "tab".
482 * If not, wraps->n_row is set to zero.
484 static int check_wraps(__isl_keep isl_mat
*wraps
, int first
,
489 for (i
= first
; i
< wraps
->n_row
; ++i
) {
490 enum isl_ineq_type type
;
491 type
= isl_tab_ineq_type(tab
, wraps
->row
[i
]);
492 if (type
== isl_ineq_error
)
494 if (type
== isl_ineq_redundant
)
503 /* Return a set that corresponds to the non-redudant constraints
504 * (as recorded in tab) of bmap.
506 * It's important to remove the redundant constraints as some
507 * of the other constraints may have been modified after the
508 * constraints were marked redundant.
509 * In particular, a constraint may have been relaxed.
510 * Redundant constraints are ignored when a constraint is relaxed
511 * and should therefore continue to be ignored ever after.
512 * Otherwise, the relaxation might be thwarted by some of
515 static __isl_give isl_set
*set_from_updated_bmap(__isl_keep isl_basic_map
*bmap
,
518 bmap
= isl_basic_map_copy(bmap
);
519 bmap
= isl_basic_map_cow(bmap
);
520 bmap
= isl_basic_map_update_from_tab(bmap
, tab
);
521 return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap
));
524 /* Given a basic set i with a constraint k that is adjacent to either the
525 * whole of basic set j or a facet of basic set j, check if we can wrap
526 * both the facet corresponding to k and the facet of j (or the whole of j)
527 * around their ridges to include the other set.
528 * If so, replace the pair of basic sets by their union.
530 * All constraints of i (except k) are assumed to be valid for j.
532 * However, the constraints of j may not be valid for i and so
533 * we have to check that the wrapping constraints for j are valid for i.
535 * In the case where j has a facet adjacent to i, tab[j] is assumed
536 * to have been restricted to this facet, so that the non-redundant
537 * constraints in tab[j] are the ridges of the facet.
538 * Note that for the purpose of wrapping, it does not matter whether
539 * we wrap the ridges of i around the whole of j or just around
540 * the facet since all the other constraints are assumed to be valid for j.
541 * In practice, we wrap to include the whole of j.
550 static int can_wrap_in_facet(struct isl_map
*map
, int i
, int j
, int k
,
551 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
554 struct isl_mat
*wraps
= NULL
;
555 struct isl_set
*set_i
= NULL
;
556 struct isl_set
*set_j
= NULL
;
557 struct isl_vec
*bound
= NULL
;
558 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
559 struct isl_tab_undo
*snap
;
562 set_i
= set_from_updated_bmap(map
->p
[i
], tabs
[i
]);
563 set_j
= set_from_updated_bmap(map
->p
[j
], tabs
[j
]);
564 wraps
= isl_mat_alloc(map
->ctx
, 2 * (map
->p
[i
]->n_eq
+ map
->p
[j
]->n_eq
) +
565 map
->p
[i
]->n_ineq
+ map
->p
[j
]->n_ineq
,
567 bound
= isl_vec_alloc(map
->ctx
, 1 + total
);
568 if (!set_i
|| !set_j
|| !wraps
|| !bound
)
571 isl_seq_cpy(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
572 isl_int_add_ui(bound
->el
[0], bound
->el
[0], 1);
574 isl_seq_cpy(wraps
->row
[0], bound
->el
, 1 + total
);
577 if (add_wraps(wraps
, map
->p
[j
], tabs
[j
], bound
->el
, set_i
) < 0)
582 snap
= isl_tab_snap(tabs
[i
]);
584 tabs
[i
] = isl_tab_select_facet(tabs
[i
], map
->p
[i
]->n_eq
+ k
);
585 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
588 isl_seq_neg(bound
->el
, map
->p
[i
]->ineq
[k
], 1 + total
);
591 if (add_wraps(wraps
, map
->p
[i
], tabs
[i
], bound
->el
, set_j
) < 0)
594 if (isl_tab_rollback(tabs
[i
], snap
) < 0)
596 if (check_wraps(wraps
, n
, tabs
[i
]) < 0)
601 changed
= fuse(map
, i
, j
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
, wraps
);
620 /* Given two basic sets i and j such that i has exactly one cut constraint,
621 * check if we can wrap the corresponding facet around its ridges to include
622 * the other basic set (and nothing else).
623 * If so, replace the pair by their union.
625 * We first check if j has a facet adjacent to the cut constraint of i.
626 * If so, we try to wrap in the facet.
634 static int can_wrap_in_set(struct isl_map
*map
, int i
, int j
,
635 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
639 unsigned total
= isl_basic_map_total_dim(map
->p
[i
]);
640 struct isl_tab_undo
*snap
;
642 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
643 if (ineq_i
[k
] == STATUS_CUT
)
646 isl_assert(map
->ctx
, k
< map
->p
[i
]->n_ineq
, return -1);
648 isl_int_add_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
649 for (l
= 0; l
< map
->p
[j
]->n_ineq
; ++l
) {
650 if (isl_tab_is_redundant(tabs
[j
], map
->p
[j
]->n_eq
+ l
))
652 if (isl_seq_eq(map
->p
[i
]->ineq
[k
],
653 map
->p
[j
]->ineq
[l
], 1 + total
))
656 isl_int_sub_ui(map
->p
[i
]->ineq
[k
][0], map
->p
[i
]->ineq
[k
][0], 1);
658 if (l
>= map
->p
[j
]->n_ineq
)
661 snap
= isl_tab_snap(tabs
[j
]);
662 tabs
[j
] = isl_tab_select_facet(tabs
[j
], map
->p
[j
]->n_eq
+ l
);
663 if (isl_tab_detect_redundant(tabs
[j
]) < 0)
666 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, NULL
, ineq_i
, NULL
, ineq_j
);
668 if (!changed
&& isl_tab_rollback(tabs
[j
], snap
) < 0)
674 /* Check if either i or j has a single cut constraint that can
675 * be used to wrap in (a facet of) the other basic set.
676 * if so, replace the pair by their union.
678 static int check_wrap(struct isl_map
*map
, int i
, int j
,
679 struct isl_tab
**tabs
, int *ineq_i
, int *ineq_j
)
683 if (count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
) == 1)
684 changed
= can_wrap_in_set(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
688 if (count(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_CUT
) == 1)
689 changed
= can_wrap_in_set(map
, j
, i
, tabs
, ineq_j
, ineq_i
);
693 /* At least one of the basic maps has an equality that is adjacent
694 * to inequality. Make sure that only one of the basic maps has
695 * such an equality and that the other basic map has exactly one
696 * inequality adjacent to an equality.
697 * We call the basic map that has the inequality "i" and the basic
698 * map that has the equality "j".
699 * If "i" has any "cut" inequality, then relaxing the inequality
700 * by one would not result in a basic map that contains the other
703 static int check_adj_eq(struct isl_map
*map
, int i
, int j
,
704 struct isl_tab
**tabs
, int *eq_i
, int *ineq_i
, int *eq_j
, int *ineq_j
)
709 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) &&
710 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
))
711 /* ADJ EQ TOO MANY */
714 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
))
715 return check_adj_eq(map
, j
, i
, tabs
,
716 eq_j
, ineq_j
, eq_i
, ineq_i
);
718 /* j has an equality adjacent to an inequality in i */
720 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_CUT
))
723 if (count(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
) != 1 ||
724 count(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) != 1 ||
725 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
) ||
726 any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
727 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
))
728 /* ADJ EQ TOO MANY */
731 for (k
= 0; k
< map
->p
[i
]->n_ineq
; ++k
)
732 if (ineq_i
[k
] == STATUS_ADJ_EQ
)
735 changed
= is_extension(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
739 changed
= can_wrap_in_facet(map
, i
, j
, k
, tabs
, eq_i
, ineq_i
, eq_j
, ineq_j
);
744 /* Check if the union of the given pair of basic maps
745 * can be represented by a single basic map.
746 * If so, replace the pair by the single basic map and return 1.
747 * Otherwise, return 0;
749 * We first check the effect of each constraint of one basic map
750 * on the other basic map.
751 * The constraint may be
752 * redundant the constraint is redundant in its own
753 * basic map and should be ignore and removed
755 * valid all (integer) points of the other basic map
756 * satisfy the constraint
757 * separate no (integer) point of the other basic map
758 * satisfies the constraint
759 * cut some but not all points of the other basic map
760 * satisfy the constraint
761 * adj_eq the given constraint is adjacent (on the outside)
762 * to an equality of the other basic map
763 * adj_ineq the given constraint is adjacent (on the outside)
764 * to an inequality of the other basic map
766 * We consider six cases in which we can replace the pair by a single
767 * basic map. We ignore all "redundant" constraints.
769 * 1. all constraints of one basic map are valid
770 * => the other basic map is a subset and can be removed
772 * 2. all constraints of both basic maps are either "valid" or "cut"
773 * and the facets corresponding to the "cut" constraints
774 * of one of the basic maps lies entirely inside the other basic map
775 * => the pair can be replaced by a basic map consisting
776 * of the valid constraints in both basic maps
778 * 3. there is a single pair of adjacent inequalities
779 * (all other constraints are "valid")
780 * => the pair can be replaced by a basic map consisting
781 * of the valid constraints in both basic maps
783 * 4. there is a single adjacent pair of an inequality and an equality,
784 * the other constraints of the basic map containing the inequality are
785 * "valid". Moreover, if the inequality the basic map is relaxed
786 * and then turned into an equality, then resulting facet lies
787 * entirely inside the other basic map
788 * => the pair can be replaced by the basic map containing
789 * the inequality, with the inequality relaxed.
791 * 5. there is a single adjacent pair of an inequality and an equality,
792 * the other constraints of the basic map containing the inequality are
793 * "valid". Moreover, the facets corresponding to both
794 * the inequality and the equality can be wrapped around their
795 * ridges to include the other basic map
796 * => the pair can be replaced by a basic map consisting
797 * of the valid constraints in both basic maps together
798 * with all wrapping constraints
800 * 6. one of the basic maps has a single cut constraint and
801 * the other basic map has a constraint adjacent to this constraint.
802 * Moreover, the facets corresponding to both constraints
803 * can be wrapped around their ridges to include the other basic map
804 * => the pair can be replaced by a basic map consisting
805 * of the valid constraints in both basic maps together
806 * with all wrapping constraints
808 * Throughout the computation, we maintain a collection of tableaus
809 * corresponding to the basic maps. When the basic maps are dropped
810 * or combined, the tableaus are modified accordingly.
812 static int coalesce_pair(struct isl_map
*map
, int i
, int j
,
813 struct isl_tab
**tabs
)
821 eq_i
= eq_status_in(map
, i
, j
, tabs
);
822 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ERROR
))
824 if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_SEPARATE
))
827 eq_j
= eq_status_in(map
, j
, i
, tabs
);
828 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ERROR
))
830 if (any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_SEPARATE
))
833 ineq_i
= ineq_status_in(map
, i
, j
, tabs
);
834 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ERROR
))
836 if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_SEPARATE
))
839 ineq_j
= ineq_status_in(map
, j
, i
, tabs
);
840 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ERROR
))
842 if (any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_SEPARATE
))
845 if (all(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_VALID
) &&
846 all(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_VALID
)) {
849 } else if (all(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_VALID
) &&
850 all(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_VALID
)) {
853 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_CUT
) ||
854 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_CUT
)) {
856 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_EQ
) ||
857 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_EQ
)) {
859 } else if (any(eq_i
, 2 * map
->p
[i
]->n_eq
, STATUS_ADJ_INEQ
) ||
860 any(eq_j
, 2 * map
->p
[j
]->n_eq
, STATUS_ADJ_INEQ
)) {
861 changed
= check_adj_eq(map
, i
, j
, tabs
,
862 eq_i
, ineq_i
, eq_j
, ineq_j
);
863 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_EQ
) ||
864 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_EQ
)) {
867 } else if (any(ineq_i
, map
->p
[i
]->n_ineq
, STATUS_ADJ_INEQ
) ||
868 any(ineq_j
, map
->p
[j
]->n_ineq
, STATUS_ADJ_INEQ
)) {
869 changed
= check_adj_ineq(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
871 changed
= check_facets(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
873 changed
= check_wrap(map
, i
, j
, tabs
, ineq_i
, ineq_j
);
890 static struct isl_map
*coalesce(struct isl_map
*map
, struct isl_tab
**tabs
)
894 for (i
= map
->n
- 2; i
>= 0; --i
)
896 for (j
= i
+ 1; j
< map
->n
; ++j
) {
898 changed
= coalesce_pair(map
, i
, j
, tabs
);
910 /* For each pair of basic maps in the map, check if the union of the two
911 * can be represented by a single basic map.
912 * If so, replace the pair by the single basic map and start over.
914 struct isl_map
*isl_map_coalesce(struct isl_map
*map
)
918 struct isl_tab
**tabs
= NULL
;
926 map
= isl_map_align_divs(map
);
928 tabs
= isl_calloc_array(map
->ctx
, struct isl_tab
*, map
->n
);
933 for (i
= 0; i
< map
->n
; ++i
) {
934 tabs
[i
] = isl_tab_from_basic_map(map
->p
[i
]);
937 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
))
938 tabs
[i
] = isl_tab_detect_implicit_equalities(tabs
[i
]);
939 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
))
940 if (isl_tab_detect_redundant(tabs
[i
]) < 0)
943 for (i
= map
->n
- 1; i
>= 0; --i
)
947 map
= coalesce(map
, tabs
);
950 for (i
= 0; i
< map
->n
; ++i
) {
951 map
->p
[i
] = isl_basic_map_update_from_tab(map
->p
[i
],
953 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
956 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_IMPLICIT
);
957 ISL_F_SET(map
->p
[i
], ISL_BASIC_MAP_NO_REDUNDANT
);
960 for (i
= 0; i
< n
; ++i
)
961 isl_tab_free(tabs
[i
]);
968 for (i
= 0; i
< n
; ++i
)
969 isl_tab_free(tabs
[i
]);
974 /* For each pair of basic sets in the set, check if the union of the two
975 * can be represented by a single basic set.
976 * If so, replace the pair by the single basic set and start over.
978 struct isl_set
*isl_set_coalesce(struct isl_set
*set
)
980 return (struct isl_set
*)isl_map_coalesce((struct isl_map
*)set
);